BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Giuse_Cava95
Calcite | Level 5

Hi all, I wanted to find the average for the past six months for every ndg (a sort of id), i have data sample like this:

datandgvalue
20160117
20160122
20160133
20160217
201602210
20160235
20160319
20160322
20160335
20160412
20160421
20160435
20160518
20160529
20160538
20160618
20160624
20160631
20160713
20160729
20160734
20160812
20160822
201608310
20160918
20160924
20160938
20161013
20161029
20161032
20161114
20161122
20161131
20161211
20161222
20161237
20170115
20170129
20170139
20170213
201702210
20170233
20170314
20170329
20170338
20170413
20170422
20170435
20170519
20170525
20170536
20170613
20170621
20170639
20170711
20170724
20170739
20170818
20170826
20170836
20170917
20170922
20170939
20171012
201710210
20171031
20171116
20171127
20171136
20171214
20171221
20171231

 

I tried using this code, but i can't group by ndgs.

 

proc sql;

create table want as
select h1.data format=yymmn6.,h1.ndg, mean(h2.value) as Value_avg,
count(*) as months

from work.test as h1 inner join work.test as h2 on
h2.data between intnx("month",h1.data,-6,"beginning") and
intnx("month",h1.data,-1,"beginning")

group by h1.data,h1.ndg;
select * from want;

quit;

 

Could you help me? Thank you all.

 

1 ACCEPTED SOLUTION

Accepted Solutions
whymath
Lapis Lazuli | Level 10
you just need add one more join condition: h1.ndg=h2.ndg

View solution in original post

3 REPLIES 3
whymath
Lapis Lazuli | Level 10
you just need add one more join condition: h1.ndg=h2.ndg
Giuse_Cava95
Calcite | Level 5
Really thank you
PaigeMiller
Diamond | Level 26

FYI

PROC EXPAND can do this as well, without the hassle of getting SQL to work properly.

 

proc sort data=have;
    by ndg month;
run;
proc expand data=have out=want;
    by ndg;
    convert value=value_6_month_avg/transformout = (movave 6);
run;

 

 

--
Paige Miller

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 516 views
  • 0 likes
  • 3 in conversation