BookmarkSubscribeRSS Feed
deepika951
Calcite | Level 5

Dear All,
I have a SAS dataset with numerator & denominator for each month-year & I would like to carry forward the sum of these numerators & denominators for upto 15 months from each row month-year value. 
I have:

Month_Yr Num Den
Dec-2018 1015

Jan-2019

4056

Feb-2019

3540
Mar-20192637
Apr-20193437
May-20196768
Jun-20192424
Jul-20195458
Aug-201956 57
Sep-20192935
Oct-20194045
Nov-20192626
Dec-20194547
Jan-20203637
Feb-20203540
Mar-20202030
Apr-20201626
May-20204650

 

I want: for each date, look back 15 months & include those values in the sum (Num_sum for numerator sums, for example). Here the 1st 15 month cut off ends at Mar 2020, so starting Apr 2020, I want the sum to include all "num" values except the Dec-2018 (as it is >15 months back in time).

Month_Yr Num DenSum_Num
Dec-2018 101510

Jan-2019

405650

Feb-2019

354085
Mar-20192637111
Apr-20193437& so on until 15th month
May-20196768 
Jun-20192424 
Jul-20195458 
Aug-201956 57 
Sep-20192935 
Oct-20194045 
Nov-20192626 
Dec-20194547 
Jan-20203637 
Feb-20203540 
Mar-20202030577
Apr-20201626567
May-20204650 

 

Is there any guidance to write a code for this?

Thank you so much.

2 REPLIES 2
Reeza
Super User
Do you have a SAS/ETS license? You can check that with the following: PROC PRODUCT_STATUS; RUN;

If you see SAS/ETS listed, you have it and can use PROC EXPAND and the CONVERT statement to get a 15 month running total. To confirm, if <15 months of data are available it just uses the data up to that point? Can you please extend your example to illustrate what happens after the 15th month so it's clear?
Astounding
PROC Star

If your data is complete ... meaning that all months are accounted for with nothing missing in the middle of the series ... the code is relatively easy:

 

data want;
   set have;
   back16 = lag16(num);
   num_sum + num;
   num_sum = sum(num_sum, -back16);
run;

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 16. 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
  • 2 replies
  • 317 views
  • 2 likes
  • 3 in conversation