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

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.

 

IDbegin dateend date
10012/6/20192/12/2019
10013/13/20193/18/2019
10024/4/20194/10/2019
10024/20/20195/12/2019
10027/11/20197/20/2019
10028/6/20198/20/2019
10028/17/20199/5/2019
10028/30/20199/13/2019
100311/7/201911/13/2019
100311/15/201912/14/2019
100312/31/20191/14/2020

 

Result data set 

 

IDbegin dateend dateDays Diff from Prev end dateContinuous Days
10012/6/20192/12/2019.7
10013/13/20193/18/2019296
10024/4/20194/10/2019.7
10024/20/20195/12/20191023
10027/11/20197/20/20196010
10028/6/20198/20/20191715
10028/17/20199/5/2019-331
10028/30/20199/13/2019-638
100311/7/201911/13/2019.7
100311/15/201912/14/2019237
100312/31/20191/14/20201715

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

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;

View solution in original post

5 REPLIES 5
novinosrin
Tourmaline | Level 20

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;
sas33
Calcite | Level 5
Hi, i don't think you have considered 7 day difference logic to be considered as continuous. Please see the highlighted rows in blue.
novinosrin
Tourmaline | Level 20

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

novinosrin
Tourmaline | Level 20

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;
sas33
Calcite | Level 5

Thank you so much, this works perfect. 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 5 replies
  • 1022 views
  • 0 likes
  • 2 in conversation