I have 2 dates (start date end date) for an encounter by patient. I have multiple encounters. How do I subtract the last date of one encounter from the first date of the next encounter for each patient?
HAVE DATASET
PATIENT ENCOUNTER START DATE END DATE
1 1 ST_DATE_1 END_DATE_1
1 2 ST_DATE_2 END_DATE_2
1 3 ST_DATE_3 END_DATE_3
1 4 ST_DATE_4 END_DATE_4
WANT DATASET
PATIENT ENCOUNTER DIFF_END DATE_START DATE_FOR CONSECUTIVE RECORDS
1 1 .
1 2 DIFF START_DATE_2 & END_DATE_1
1 3 DIFF START_DATE_3 & END_DATE_2
1 4 DIFF START_DATE_4 & END_DATE_3
First requirement: Have the dates as numeric values, and best a SAS format that displays them as a usable date. Note: a number like 20201102 is not a SAS date.
data want; set have; by patient;
lend = lag(end_date); if not first.patient then daysdiff = Start_date - Lend;
drop lend; run;
The LAG function maintains a queue of prior values so you can access them.
When using BY group in a data set SAS creates automatic variables First. and Last. for each variable on the By statement that you can test for true/false.
First requirement: Have the dates as numeric values, and best a SAS format that displays them as a usable date. Note: a number like 20201102 is not a SAS date.
data want; set have; by patient;
lend = lag(end_date); if not first.patient then daysdiff = Start_date - Lend;
drop lend; run;
The LAG function maintains a queue of prior values so you can access them.
When using BY group in a data set SAS creates automatic variables First. and Last. for each variable on the By statement that you can test for true/false.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.