BookmarkSubscribeRSS Feed
mak2145
Calcite | Level 5

I have some basic code that takes a percent of the previous days value and carries it over to the next day using lag(). I have certain dynamic days (like christmas) that I would like to passover and skip the lag() and put it on the following day breaking the normal lag1().

 

Any ideas how I can do this?

DateX (random)25% of previous totalTotal 
12/16/202214 14.0 
12/17/2022113.514.5 
12/18/202283.611.6 
12/19/202252.97.9 
12/20/202222.04.0 
12/21/202271.08.0 
12/22/2022142.016.0 
12/23/202294.013.0 
12/24/202263.29.2 
12/25/2022--- 
12/26/202252.37.3Skips 12/25
12/27/202261.87.8 
12/28/202282.010.0 

 

2 REPLIES 2
ballardw
Super User

If you show your current code using the lags and the list of dates or perhaps Holidays that need such treatment you may get a better answer.

 

Generic response: request enough lags for any of the periods you might need.

On given dates then conditionally use the needed lags.

 

data generic;
   set have;
   l1=Lag1(value);
   l2=Lag2(value);
   <repeat as needed>
   if (month(date)=12 and day(date)=26)
/* or other dates that require skipping the day before*/
    then (code to use L2 through however many is needed);
   else do <the code for using the previous date)
   end;
run;
  

@mak2145 wrote:

I have some basic code that takes a percent of the previous days value and carries it over to the next day using lag(). I have certain dynamic days (like christmas) that I would like to passover and skip the lag() and put it on the following day breaking the normal lag1().

 

Any ideas how I can do this?

Date X (random) 25% of previous total Total  
12/16/2022 14   14.0  
12/17/2022 11 3.5 14.5  
12/18/2022 8 3.6 11.6  
12/19/2022 5 2.9 7.9  
12/20/2022 2 2.0 4.0  
12/21/2022 7 1.0 8.0  
12/22/2022 14 2.0 16.0  
12/23/2022 9 4.0 13.0  
12/24/2022 6 3.2 9.2  
12/25/2022 - - -  
12/26/2022 5 2.3 7.3 Skips 12/25
12/27/2022 6 1.8 7.8  
12/28/2022 8 2.0 10.0  

 


 

Quentin
Super User

Please show your current code with a simple lag1 with no 'skips'.  This could be a case where you could use a conditional lag.  So if X=. indicates the records that should be skipped, your lag might be:

if not missing(x) then lag_total=lag(total) ;

You will see plenty of tips where people say "never use lag conditionally," but I think this is a case where conditional lag will do what you want.  The key point is that lag1() does not give you the value from the row immediately before, it gives you the value that was stored the last time lag1() was executed.

BASUG is hosting free webinars Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 241 views
  • 2 likes
  • 3 in conversation