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!
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.