|
Re: 练手系列2:移动平均计算
刚时成功,最笨的方法来了,呵呵!!
[code:3s74yr8m]data a;
infile datalines;
input cid month $ balance;
datalines;
1 200001 1
1 200002 1
1 200003 2
1 200004 .
1 200005 1
1 200006 1
1 200007 1
1 200008 1
1 200009 1
1 200010 1
1 200011 1
1 200012 1
1 200101 1
1 200102 1
1 200103 1
1 200104 1
1 200105 1
1 200106 1
1 200107 1
1 200108 1
1 200109 1
2 200001 1
2 200002 1
2 200003 2
2 200004 .
2 200005 1
2 200006 1
2 200007 1
2 200008 1
2 200009 1
;
run;
proc sort data=a;
by cid month;
run;
data b;
set a;
by cid;
retain total;
if first.cid=1 then do;
total=0;
end;
if balance ne . then total=total+balance;
run;
data c;
set b;
by cid;
if first.cid=1 then do;
mv6=0;
mv12=0;
retain i k 0;
i=1;
end;
if i<12 and balance=. then k=k+1;
if first.cid ne 1 then do;
if i<6 then do;
mv6=lag(total)/(i-k);
end;
else do;
mv6=mean(lag1(balance),lag2(balance),lag3(balance),lag4(balance),lag5(balance),lag6(balance));
end;
if i<12 then do;
mv12=lag(total)/(i-k);
end;
else do;
mv12=mean(lag1(balance),lag2(balance),lag3(balance),lag4(balance),lag5(balance),
lag6(balance),lag7(balance),lag8(balance),lag9(balance),lag10(balance),lag11(balance),lag12(balance));
end;
i=i+1;
end;
if last.cid=1 then k=0;
run;
[/code:3s74yr8m] |
|