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

 

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)"

 

ClientVisitsStart DateEnd DateTime between episodes (days)
111/11/20193/11/2019-
125/11/201911/11/20192
125/11/201911/11/20192
213/11/20193/11/2019-
315/11/20197/11/2019-
315/11/20197/11/2019-
315/11/20197/11/2019-
3210/11/201915/11/20193
3210/11/201915/11/20193
1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

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;
Thanks,
Jag

View solution in original post

2 REPLIES 2
Jagadishkatam
Amethyst | Level 16

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;
Thanks,
Jag
Sapt86
Fluorite | Level 6

Hi Jag, 

 

Thank you so much for your help! This worked perfectly fine for me. Thanks!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 574 views
  • 2 likes
  • 2 in conversation