BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hi everyone!!

I'm a spanish student, and I need to scan a table in SAS, register by register, to make an update on every row (depending of several conditions that include variables in previous row)

For example, I want to modify the field Date2 in this example with de field Date of the previous field, if the previous row is the same ID:

Initially:

ID DATE DATE2
1 "25-Oct-2000" ""
1 "30-Oct-2001" ""
1 "25-Nov-2002" ""
2 "13-Jan-2000" ""
2 "15-Mar-2001" ""

Finally

ID DATE DATE2
1 "25-Oct-2000" ""
1 "30-Oct-2001" "25-Oct-2000"
1 "25-Nov-2002" "30-Oct-2001"
2 "13-Jan-2000" ""
2 "15-Mar-2001" "13-Jan-2000"

Thanks and sorry for my english (is very poor)!!!!!
3 REPLIES 3
deleted_user
Not applicable
Hi, not sure how to do it in Proc SQL, but in base SAS it can be done this way:

Data ;
Set ;
By Id;
DATE2=lag(DATE);
If first.ID then DATE2="";
Run;
deleted_user
Not applicable
Thanks!!

One doubt more: with your code, the last row in the final table is deleted. do you know why?
deleted_user
Not applicable
Fixed!!!

Other question. How can I define and work with a variable into a data segment?

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 3 replies
  • 1411 views
  • 0 likes
  • 1 in conversation