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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.