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.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.