hurley18 发表于 2022-1-19 16:16
大神,看看这个例子能否借鉴。可以实现跳跃性跨组取值计算
proc sort data=stock.b out=b;
by stockcode wk date;
run;
data b_1(drop=wn1 wn2 wn3 wn4) b_2(keep=wn1 wn2 wn3 wn4);
set b;
by stockcode wk date;
if first.stockcode then nw=0;
if first.wk then nw+1;
if last.wk then do;
wnclose=nclose;
wn1=lag(nclose);
wn2=lag2(nclose);
wn3=lag3(nclose);
wn4=lag4(nclose);
end;
run;
data bn;
set b_1;
set b_2(firstobs=5);
retain lw1 lw2 lw3 lw4;
array lw{4};
array wn{4};
do i=1 to 4;
if ^missing(wn{i}) then lw{i}=wn{i};
end;
if nw>=5 then wma5=mean(of lw1-lw4,nclose);
drop i wn: lw:;
run;