| gvkey | fyear | at | want |
| 1001 | 2001 | 12.3 | . |
| 1001 | 2002 | 13.6 | 12.3 |
| 1001 | 2003 | 12.6 | 13.6 |
| 1001 | 2004 | 11.9 | 12.6 |
| 1002 | 2001 | 13.4 | . |
| 1002 | 2002 | 17.7 | 13.4 |
| 1002 | 2003 | 18.4 | 17.7 |
| 1002 | 2004 | 15.1 | 18.4 |
data want;
set have;
by gvkey fyear;
if first.gvkey then want=.;
else want=lag(at);
run;
My data is already sorted. I am not getting the expected result. What is the mistake here?
Never use LAG conditionally (unless you really know what you are doing). Slightly change your code:
data want;
set have;
by gvkey fyear;
want = lag(at);
if first.gvkey then want = .;
run;
This is necessary because LAG will only put a value into the queue when it is called.
@abdulla Can you include both your input data and your expected result?
Never use LAG conditionally (unless you really know what you are doing). Slightly change your code:
data want;
set have;
by gvkey fyear;
want = lag(at);
if first.gvkey then want = .;
run;
This is necessary because LAG will only put a value into the queue when it is called.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!
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.
Ready to level-up your skills? Choose your own adventure.