Hi everyone,
I'm quite new with SAS, and I wonder if you can please help me.
I'm trying to calculate time in days between the visits to the hospital of each client. That is, the idea is to know how many days pass between the client ends treatment and begins with a new treatment.
In my example, what I need, is the variable "Time between episodes (days)"
Client | Visits | Start Date | End Date | Time between episodes (days) |
1 | 1 | 1/11/2019 | 3/11/2019 | - |
1 | 2 | 5/11/2019 | 11/11/2019 | 2 |
1 | 2 | 5/11/2019 | 11/11/2019 | 2 |
2 | 1 | 3/11/2019 | 3/11/2019 | - |
3 | 1 | 5/11/2019 | 7/11/2019 | - |
3 | 1 | 5/11/2019 | 7/11/2019 | - |
3 | 1 | 5/11/2019 | 7/11/2019 | - |
3 | 2 | 10/11/2019 | 15/11/2019 | 3 |
3 | 2 | 10/11/2019 | 15/11/2019 | 3 |
please try
data have;
input Client Visits Start_Date:ddmmyy10. End_Date:ddmmyy10.;
cards;
1 1 1/11/2019 3/11/2019
1 2 5/11/2019 11/11/2019
1 2 5/11/2019 11/11/2019
2 1 3/11/2019 3/11/2019
3 1 5/11/2019 7/11/2019
3 1 5/11/2019 7/11/2019
3 1 5/11/2019 7/11/2019
3 2 10/11/2019 15/11/2019
3 2 10/11/2019 15/11/2019
;
data want;
set have;
by client visits Start_Date;
retain sdate edate;
if first.visits then edate= End_Date;
if first.visits then sdate=lag(edate)-Start_Date;
if first.client then sdate=.;
format Start_Date End_Date edate date9.;
run;
please try
data have;
input Client Visits Start_Date:ddmmyy10. End_Date:ddmmyy10.;
cards;
1 1 1/11/2019 3/11/2019
1 2 5/11/2019 11/11/2019
1 2 5/11/2019 11/11/2019
2 1 3/11/2019 3/11/2019
3 1 5/11/2019 7/11/2019
3 1 5/11/2019 7/11/2019
3 1 5/11/2019 7/11/2019
3 2 10/11/2019 15/11/2019
3 2 10/11/2019 15/11/2019
;
data want;
set have;
by client visits Start_Date;
retain sdate edate;
if first.visits then edate= End_Date;
if first.visits then sdate=lag(edate)-Start_Date;
if first.client then sdate=.;
format Start_Date End_Date edate date9.;
run;
Hi Jag,
Thank you so much for your help! This worked perfectly fine for me. Thanks!
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 16. 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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: