BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
abdulla
Pyrite | Level 9
gvkeyfyearatwant
1001200112.3.
1001200213.612.3
1001200312.613.6
1001200411.912.6
1002200113.4.
1002200217.713.4
1002200318.417.7
1002200415.118.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? 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

View solution in original post

5 REPLIES 5
mklangley
Lapis Lazuli | Level 10

@abdulla   Can you include both your input data and your expected result?

abdulla
Pyrite | Level 9
The first three columns are my data and the fourth column is my expected result.
Reeza
Super User
Lag cannot be calculated conditionally, so calculate it all the time and then set it to missing AFTER.

want = lag(at);
if first.gvkey then call missing(want);
Kurt_Bremser
Super User

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
Pyrite | Level 9
Thank you

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 771 views
  • 2 likes
  • 4 in conversation