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.
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.