Hello,
I'm wondering if there is a way to calculate the difference between two dates in different variables listed in different rows. My data looks like below:
MemberID InDate OutDate
1 1/1/2019 1/3/2019
1 1/4/2019 1/7/2019
2 1/15/2019 1/16/2019
2 1/20/2019 1/25/2019
Result that I'd like to have is
MemberID InDate OutDate IntervalDays
1 1/1/2019 1/3/2019 .
1 1/4/2019 1/7/2019 1 (difference between 1/4/2019 and 1/3/2019)
2 1/15/2019 1/16/2019 .
2 1/20/2019 1/25/2019 4(difference between 1/20/2019 and 1/16/2019)
Any advice will be helpful!!
Thank you!
Please try
data have;
input MemberID InDate:mmddyy10. OutDate:mmddyy10.;
format InDate OutDate date9.;
cards;
1 1/1/2019 1/3/2019
1 1/4/2019 1/7/2019
2 1/15/2019 1/16/2019
2 1/20/2019 1/25/2019
;
proc sort data=have;
by memberid indate outdate;
run;
data want;
set have;
by memberid indate outdate;
intervaldays=InDate-lag(OutDate);
if first.memberid then intervaldays=.;
run;
Use the lag() function to retrieve the value of a variable from the preceding observation.
Please try
data have;
input MemberID InDate:mmddyy10. OutDate:mmddyy10.;
format InDate OutDate date9.;
cards;
1 1/1/2019 1/3/2019
1 1/4/2019 1/7/2019
2 1/15/2019 1/16/2019
2 1/20/2019 1/25/2019
;
proc sort data=have;
by memberid indate outdate;
run;
data want;
set have;
by memberid indate outdate;
intervaldays=InDate-lag(OutDate);
if first.memberid then intervaldays=.;
run;
Thank you!
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!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: