BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
NickS2
Obsidian | Level 7

I am getting unusual results when I am using the lag function and not getting the results I am looking for. 

 

Dataset:

 

ID   Event_Date

1     12MAY2020

1      24JUN2020

1      08AUG2020

2      09APR2020

3      26JUN2020

3      30AUG2020

 

Program I wrote:

data want;

     set have;

     by ID;

     if first.ID then prev_event_dt = .;

     else prev_event_dt=lag(Event_Date);

run;

 

Output I am expecting:

ID   Event_Date               prev_event_dt 

1     12MAY2020              .

1      24JUN2020              12MAY2020

1      08AUG2020              24JUN2020

2      09APR2020              .

3      26JUN2020              .

3      30AUG2020             26JUN2020              

 

Please point out where I am going wrong with the program I wrote.

 

Thank you

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You never passed in the date from the first observation per ID into the queue/stack that LAG() uses. 

So it is not there to be returned later.

Tom_0-1656438561790.png

In general you never want to conditionally run the LAG() function because of this issue.

data want;
   set have;
   by ID;
   prev_event_dt=lag(Event_Date);
   if first.ID then prev_event_dt = .;
run;

 

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

LAG does not behave as you expect it in IF / THEN / ELSE statements. (It's doing the right thing, but your expectation is wrong)

 

Better you should use the IFN function (or IFC if you want to create a character variable)

 

prev_event_dt=ifn(first.id,.,lag(event_date));

 

 

--
Paige Miller
Tom
Super User Tom
Super User

You never passed in the date from the first observation per ID into the queue/stack that LAG() uses. 

So it is not there to be returned later.

Tom_0-1656438561790.png

In general you never want to conditionally run the LAG() function because of this issue.

data want;
   set have;
   by ID;
   prev_event_dt=lag(Event_Date);
   if first.ID then prev_event_dt = .;
run;

 

Kurt_Bremser
Super User

The LAG function creates a FIFO queue that is filled every time the function is called. Calling LAG in a conditional branch means that not all values will make it into the queue.

Restructure your code:

data want;
set have;
by ID;
prev_event_dt = lag(Event_Date);
if first.ID then prev_event_dt = .;
run;

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
  • 3 replies
  • 571 views
  • 2 likes
  • 4 in conversation