BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Deep_Impact
Calcite | Level 5

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

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

View solution in original post

2 REPLIES 2
ballardw
Super User

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.

Deep_Impact
Calcite | Level 5
Hi ballardw,

Thank you for your prompt reply. It did work and got what I wanted. Only thing in the program log was that first.patient is uninitialized.

Ready to join fellow brilliant minds for the SAS Hackathon?

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!
How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 722 views
  • 2 likes
  • 2 in conversation