BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Cruiser13
Fluorite | Level 6

I have a dataset that has a Year column and a Death column. I want to create 3 year average deaths column (where the first and last years just have the single year's deaths).

 

For example:

 

yeardeaths3 year average deaths
200966
2010178.666666667
2011316.33333333
20122925
20134329
20141521.66666667
201578.666666667
201647.333333333
2017119.333333333
20181315.33333333
20192221.66666667
20203030

 

So the entry for the 3 year average deaths for 2010 is (6+17+3)/3=8.666666667. 

 

How would I do this in SAS?

 

Thanks in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

If you have SAS/ETS, which contains PROC EXPAND, you can use the solution provided by @sbxkoenk.

 

Otherwise a little data step programming is needed, with code to include the preceding, current, and subsequent death counts.

 

data work.deaths;
  informat year $4.;
  input year $ deaths ; 
  date=mdy(12,31,input(year,4.));
  format date date9.;
cards;
2009 6	
2010 17	
2011 3	
2012 29	
2013 43	
2014 15	
2015 7	
2016 4	
2017 11	
2018 13	
2019 22	
2020 30	
run;

data want (drop=_:);
  merge deaths
        deaths (firstobs=2 keep=deaths rename=(deaths=_nxt_deaths)) 
  end=lastrec;
  centered_moving_average=mean(lag(deaths),deaths,_nxt_deaths);
  if _n_=1 or lastrec=1 then centered_moving_average=deaths;
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

3 REPLIES 3
sbxkoenk
SAS Super FREQ

Hello,

 

What you are after is a centered moving average.

You can do that with the CMOVAVE transformation operation in PROC EXPAND (you need SAS/ETS).

data work.deaths;
informat year $4.;
input year $ deaths three3_year_average_deaths;
date=mdy(12,31,input(year,4.));
format date date9.;
cards;
2009 6	6
2010 17	8.666666667
2011 3	16.33333333
2012 29	25
2013 43	29
2014 15	21.66666667
2015 7	8.666666667
2016 4	7.333333333
2017 11	9.333333333
2018 13	15.33333333
2019 22	21.66666667
2020 30	30
;
run;

proc expand data=work.deaths out=work.death_exp method=none;
   id date;
   convert deaths = deaths_exp / transformout=(cmovave 3);
run;
/* end of program */

Cheers,

Koen

mkeintz
PROC Star

If you have SAS/ETS, which contains PROC EXPAND, you can use the solution provided by @sbxkoenk.

 

Otherwise a little data step programming is needed, with code to include the preceding, current, and subsequent death counts.

 

data work.deaths;
  informat year $4.;
  input year $ deaths ; 
  date=mdy(12,31,input(year,4.));
  format date date9.;
cards;
2009 6	
2010 17	
2011 3	
2012 29	
2013 43	
2014 15	
2015 7	
2016 4	
2017 11	
2018 13	
2019 22	
2020 30	
run;

data want (drop=_:);
  merge deaths
        deaths (firstobs=2 keep=deaths rename=(deaths=_nxt_deaths)) 
  end=lastrec;
  centered_moving_average=mean(lag(deaths),deaths,_nxt_deaths);
  if _n_=1 or lastrec=1 then centered_moving_average=deaths;
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Cruiser13
Fluorite | Level 6

Thank you, this worked!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 418 views
  • 5 likes
  • 3 in conversation