Hi,
If you have SAS/ETS, check out the documentation on Proc Expand, which has been built in with such a functionality. Here is to show you one approach with data step:
filename test 'c:\temp\test.csv';
data have;
infile test dsd truncover firstobs=2;
input gvkey$ lpermno$ fyear PER_CHANGE;
run;
data want;
array t(0:4) _temporary_;
do _n_=1 by 1 until (last.lpermno);
set have;
by lpermno notsorted;
t(mod(_n_,5))=per_change;
if _n_>=5 then mov5avg=mean(of t(*));
output;
end;
call missing(of t(*));
run;
Haikuo