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.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 1878 views
  • 8 likes
  • 9 in conversation