Good afternoon,
I may have a problem to generate new data. As we can see that after Jan-01-2014, Rev is 0. And I would like to fill Rev after Jan-01-2014 by calculating: Rev=lag2(Rev)*Exp(Num). However, there is no data for Rev after Jan-01-2014, so SAS cannot calculate Rev based on the lagged Rev. I tried Retain but it didn't work:(
Num | Rev | Date |
3 | 45454 | Jan-01-2013 |
4 | 354353 | Feb-01-2013 |
7 | 25425 | Mar-01-2013 |
3 | 254235 | Jan-01-2014 |
8 | Feb-01-2014 | |
9 | Mar-01-2014 | |
12 | Jan-01-2015 | |
45 | Feb-01-2015lag |
Yes, that would be a problem. LAG works on the original values when the LAG function is invoked, rather than the subsequent values after manipulating the data.
Here's a workaround, creating your own stream of values that mimics what the LAG function would do but after manipulating the data.
data want;
back2 = back1;
back1 = rev;
retain back1;
set have;
if rev=. then rev=back2 * exp(num);
* optionally, once the program is working, drop back1 back2;
run;
Good luck.
Yes, that would be a problem. LAG works on the original values when the LAG function is invoked, rather than the subsequent values after manipulating the data.
Here's a workaround, creating your own stream of values that mimics what the LAG function would do but after manipulating the data.
data want;
back2 = back1;
back1 = rev;
retain back1;
set have;
if rev=. then rev=back2 * exp(num);
* optionally, once the program is working, drop back1 back2;
run;
Good luck.
Great thanks Astounding, but what if I need lag4 instead of lag2, what I shall change the code?
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.