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.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.