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.

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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