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.
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: