Thank you Mike and Astounding! I am happy because my code is the shortest:smileysilly::smileylaugh:!
Linlin
Linlin,
Your comment reminds me of a smallest program challenge, on SAS-L, some years ago. Take a look at:
Art,
I have looked at the link. Too bad you did not win.
Linlin: ah, but I did (in a sense). That, and about another gazillion similar posts, got me into the SAS-L Hall of Fame (see: http://www.sascommunity.org/wiki/SAS-L_BOF )
For your question, I would like to use two skill: self-merge or SQL .
I prefer to SQL , because it might be faster when your table is large. Just a guess .
data have; input x; datalines; 1 2 3 4 5 6 7 8 9 10 ; run; /*self-merge skill*/ data want; merge have have(firstobs=2 rename=(x=x1)) have(firstobs=3 rename=(x=x2)) have(firstobs=4 rename=(x=x3)) have(firstobs=5 rename=(x=x4)) have(firstobs=6 rename=(x=x5)); avg=mean(of x1-x5); drop x1-x5; run; /*SQL skill*/ data have; set have; n=_n_; run; proc sql; create table want(drop=n) as select *,(select mean(x) from have where n between a.n+1 and a.n+5) as avg from have as a; quit;
Ksharp
And another way to do it, no arrays, just lag.
data temp (keep=MEAN5_X);
set have;
retain MEAN5_X .;
MEAN5_X=sum(MEAN5_X,X,-lag5(X));
if _N_ gt 5;
run;
data want;
merge have temp;
MEAN5_X=MEAN5_X/5;
run;
Cheers from Portugal.
Daniel Santos @ www.cgd.pt
Thank you all for providing so many ideas. Thanks.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.