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 |
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
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.
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!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: