Calcite | Level 5

## Number of Continuous Days

Hi,

i want to calculate  Continuous days on a drug by id.

if the days difference between previous end date  & next start date <= 7 days then it is considered as continuous.

 ID begin date end date 1001 2/6/2019 2/12/2019 1001 3/13/2019 3/18/2019 1002 4/4/2019 4/10/2019 1002 4/20/2019 5/12/2019 1002 7/11/2019 7/20/2019 1002 8/6/2019 8/20/2019 1002 8/17/2019 9/5/2019 1002 8/30/2019 9/13/2019 1003 11/7/2019 11/13/2019 1003 11/15/2019 12/14/2019 1003 12/31/2019 1/14/2020

Result data set

 ID begin date end date Days Diff from Prev end date Continuous Days 1001 2/6/2019 2/12/2019 . 7 1001 3/13/2019 3/18/2019 29 6 1002 4/4/2019 4/10/2019 . 7 1002 4/20/2019 5/12/2019 10 23 1002 7/11/2019 7/20/2019 60 10 1002 8/6/2019 8/20/2019 17 15 1002 8/17/2019 9/5/2019 -3 31 1002 8/30/2019 9/13/2019 -6 38 1003 11/7/2019 11/13/2019 . 7 1003 11/15/2019 12/14/2019 2 37 1003 12/31/2019 1/14/2020 17 15

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Tourmaline | Level 20

## Re: Number of Continuous Days

Hi @sas33  Please see if the revised one helps

``````data want;
_n_=.;
do until(last.id);
set have;
by id;
if first.id then _b=begindate;
if _n_ then daysdiff=begindate-_n_;
if  daysdiff>7 then _b= begindate;
Continuous =enddate-_b+1;
_n_=enddate;
output;
end;
drop _:;
run;``````
5 REPLIES 5
Tourmaline | Level 20

## Re: Number of Continuous Days

HI @sas33

``````
data have;
input ID	(begindate	enddate) (:mmddyy10.);
format begindate	enddate mmddyy10.;
cards;
1001	2/6/2019	2/12/2019
1001	3/13/2019	3/18/2019
1002	4/4/2019	4/10/2019
1002	4/20/2019	5/12/2019
1002	7/11/2019	7/20/2019
1002	8/6/2019	8/20/2019
1002	8/17/2019	9/5/2019
1002	8/30/2019	9/13/2019
1003	11/7/2019	11/13/2019
1003	11/15/2019	12/14/2019
1003	12/31/2019	1/14/2020
;

data want;
_n_=.;
do until(last.id);
set have;
by id;
Continuous =enddate-begindate+1;
if _n_ then daysdiff=begindate-_n_;
_n_=enddate;
output;
end;
run;``````
Calcite | Level 5

## Re: Number of Continuous Days

Hi, i don't think you have considered 7 day difference logic to be considered as continuous. Please see the highlighted rows in blue.
Tourmaline | Level 20

## Re: Number of Continuous Days

My sincere apologies for overlooking that part. Let me revisit the thread

Tourmaline | Level 20

## Re: Number of Continuous Days

Hi @sas33  Please see if the revised one helps

``````data want;
_n_=.;
do until(last.id);
set have;
by id;
if first.id then _b=begindate;
if _n_ then daysdiff=begindate-_n_;
if  daysdiff>7 then _b= begindate;
Continuous =enddate-_b+1;
_n_=enddate;
output;
end;
drop _:;
run;``````
Calcite | Level 5

## Re: Number of Continuous Days

Thank you so much, this works perfect.

Discussion stats
• 5 replies
• 768 views
• 0 likes
• 2 in conversation