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

So I am trying to do the rolling sum (5 points) in SAS UE adding lags. I also want to get rid of missings, but they are still generated. What is wrong?

I tried doing if statements separately but didn't work...


data SUM;
set work.IMPORT;
rollingsum = y+lag1(y)+lag2(y)+lag3(y)+lag4(y);
if x=-10 then rollingsum = y;
if x=(-9.9) then rollingsum = y+lag1(y);
if x=(-9.8) then rollingsum = y+lag1(y)+lag2(y);
if x=(-9.7) then rollingsum = y+lag1(y)+lag2(y)+lag3(y);
run;

 

obs x y rollingsum
1 -10 0.5440211109 0.5440211109
2 -9.9 0.4575358938 .
3 -9.8 0.3664791293 .
4 -9.7 0.2717606264 .
5 -9.6 0.1743267812 1.8141235416
6 -9.5 0.0751511205 1.3452535511
7 -9.4 -0.024775425 0.8629422319
8 -9.3 -0.124454424 0.3720086791
9 -9.2 -0.222889914 -0.122641861 


I want to have

obs x y rollingsum
1 -10 0.5440211109 0.5440211109
2 -9.9 0.4575358938 0.5440211109 + 0.4575358938
3 -9.8 0.3664791293 0.5440211109 + 0.4575358938 + 0.3664791293
4 -9.7 0.2717606264 ...+ 0.2717606264
5 -9.6 0.1743267812 1.8141235416
6 -9.5 0.0751511205 1.3452535511
7 -9.4 -0.024775425 0.8629422319
8 -9.3 -0.124454424 0.3720086791
9 -9.2 -0.222889914 -0.122641861

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Use the SUM function instead of +. When you use the plus sign and any of the values are missing the result will be missing.

However if you use

if x=(-9.7) then rollingsum = sum (y,lag1(y),lag2(y),lag3(y));

that doesn't happen.

However you will run into a classic issue about use of LAG with IF. The lag function retains separate queues.

You would actually do better with

 

data SUM;
   set work.IMPORT;
   ly1 = lag1(y);
   ly2 = lag2(y);
   ly3 = lag3(y);
   ly4 = lag4(y);

   rollingsum = sum(y, ly1, ly2, ly3, lyr);
   if x=-10 then rollingsum = y;
   if x=(-9.9) then rollingsum = sum(y, ly1);
   if x=(-9.8) then rollingsum = sum(y, ly1, ly2);
   if x=(-9.7) then rollingsum = sum(y, ly1, ly2, ly3);
   drop ly1 - ly4;
run;

You could run that code without the drop statement to see if the proper values of y are being used.

View solution in original post

3 REPLIES 3
ballardw
Super User

Use the SUM function instead of +. When you use the plus sign and any of the values are missing the result will be missing.

However if you use

if x=(-9.7) then rollingsum = sum (y,lag1(y),lag2(y),lag3(y));

that doesn't happen.

However you will run into a classic issue about use of LAG with IF. The lag function retains separate queues.

You would actually do better with

 

data SUM;
   set work.IMPORT;
   ly1 = lag1(y);
   ly2 = lag2(y);
   ly3 = lag3(y);
   ly4 = lag4(y);

   rollingsum = sum(y, ly1, ly2, ly3, lyr);
   if x=-10 then rollingsum = y;
   if x=(-9.9) then rollingsum = sum(y, ly1);
   if x=(-9.8) then rollingsum = sum(y, ly1, ly2);
   if x=(-9.7) then rollingsum = sum(y, ly1, ly2, ly3);
   drop ly1 - ly4;
run;

You could run that code without the drop statement to see if the proper values of y are being used.

PaigeMiller
Diamond | Level 26

Set the missings to zero, and then when you do a sum and add zero into the sum ... well, you know ...

--
Paige Miller
likich
Calcite | Level 5
It is brilliant! Thank you so much!

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
  • 3 replies
  • 369 views
  • 1 like
  • 3 in conversation