Hi all
I need help, I’m looking to calculate the 5 year average for per_change. This five year average must be specific for each company identified by the permn number, and have consecutive years
EG:
Permno | year | Per_change | 5yravg |
10001 | 2001 | 0.2 | |
10001 | 2002 | 0.6 | |
10001 | 2003 | 0.4 | |
10001 | 2004 | 0.22 | |
10001 | 2005 | 0.3 | 0.344 |
10001 | 2006 | 0.5 | 0.404 |
I have attached my data any help in this command would be greatly appreciated.
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
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.