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

I don't necessarily need a coding solution, but am looking to understand why this code doesn't work.

data want; set have; 

  if id=lag(id) then last_visit=lag(visit);

run;

Have:

idvisit
27/5/2014
28/30/2014
29/24/2014
4
84/25/2014
84/26/2014
85/28/2014



Want:

idvisitlast_visit_date
27/5/2014
28/30/20147/5/2014
29/24/20148/30/2014
4
84/25/2014
84/26/20144/25/2014
85/28/20144/26/2014

Actual Result:

idvisitlast_visit_date
27/5/2014
28/30/20147/5/2014
29/24/20148/30/2014
4
84/25/2014
84/26/20149/24/2014
85/28/20144/26/2014

I believe I could also accomplish this with a retain statement, but it eventually worked after outright creating the variable lag_visit (below).   I don't completely understand why it was necessary to do this.

data want; set have; 

     lag_visit=lag1(visit_dt);

     if id=lag(id) then last_visit=lag_visit;

run;

Thanks,

K

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Lag function creates a queue. If you execute it conditionally, then it isn't updated every iteration.

In general, don't use Lag functions in conditional statements.

This paper illustrates your issue and explains it better than I can Smiley Happy

http://www.howles.com/saspapers/CC33.pdf

View solution in original post

2 REPLIES 2
Reeza
Super User

Lag function creates a queue. If you execute it conditionally, then it isn't updated every iteration.

In general, don't use Lag functions in conditional statements.

This paper illustrates your issue and explains it better than I can Smiley Happy

http://www.howles.com/saspapers/CC33.pdf

moreka
Obsidian | Level 7

Thanks -- exactly what I was looking for!

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 2 replies
  • 940 views
  • 0 likes
  • 2 in conversation