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
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;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;My sincere apologies for overlooking that part. Let me revisit the thread
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;Thank you so much, this works perfect.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.
