SAS Programming

DATA Step, Macro, Functions and more
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-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1318 views
  • 2 likes
  • 4 in conversation