/* Compute the moving average within a BY-group of last n observations.
For the first n-1 observations within the BY-group, the moving average
is set to missing. */
data ds1;
do patient='A','B','C';
do month=1 to 7;
num=int(ranuni(0)*10);
output;
end;
end;
run;
proc sort;
by patient;
%let n = 4;
data ds2;
set ds1;
by patient;
retain num_sum 0;
if first.patient then do;
count=0;
num_sum=0;
end;
count+1;
last&n=lag&n(num);
if count gt &n then num_sum=sum(num_sum,num,-last&n);
else num_sum=sum(num_sum,num);
if count ge &n then mov_aver=num_sum/&n;
else mov_aver=.;
run;
proc print;
run;
不知道紅色那句是什么意思呢?
根據(jù)這個(gè)程序,能不能改編成moving standard deviation of last n observation.覺(jué)得貌似可以,但是好像又沒(méi)那么簡(jiǎn)單。不知大家有何見(jiàn)解?