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.

The Boston Area SAS Users Group is hosting free webinars!
Next up: Lisa Mendez & Richann Watson present Get Tipsy with Debugging Tips for SAS® Code: The After Party on Wednesday Jul 16.
Register now at https://www.basug.org/events.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 537 views
  • 2 likes
  • 3 in conversation