BookmarkSubscribeRSS Feed
Linlin
Lapis Lazuli | Level 10

Thank you Mike and Astounding! I am happySmiley Happy because my code is the shortest:smileysilly::smileylaugh:!

Linlin

art297
Opal | Level 21

Linlin,

Your comment reminds me of a smallest program challenge, on SAS-L, some years ago.  Take a look at:

Search results -- SAS-L</title><style type="text/css"><!--BODY { font-family: "Comic Sans MS",arial,...

Linlin
Lapis Lazuli | Level 10

Art,

I have looked at the link. Too bad you did not winSmiley Sad.

art297
Opal | Level 21

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 )

Ksharp
Super User

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

DanielSantos
Barite | Level 11

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

TomiKong
Fluorite | Level 6

Thank you all for providing so many ideas. Thanks.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

What is Bayesian Analysis?

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 21 replies
  • 2978 views
  • 8 likes
  • 9 in conversation