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

## Calculate the time between the visits by group

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
1 ACCEPTED SOLUTION

Accepted Solutions
Amethyst | Level 16

## Re: Calculate the time between the visits by group

``````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
2 REPLIES 2
Amethyst | Level 16

## Re: Calculate the time between the visits by group

``````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
Fluorite | Level 6

## Re: Calculate the time between the visits by group

Hi Jag,

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

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