<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Calculating Cumulative days with Overlap in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/573362#M161866</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/250829"&gt;@serena13lee&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you just need the&amp;nbsp;cumulative number of days on &lt;EM&gt;any&lt;/EM&gt; drug, you can keep the code simple (at the expense of several seconds of run time for an input dataset with, say, 500,000 observations from 50,000 patients with a mean duration of 10 years on drug):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Determine earliest start date and latest end date */

proc sql noprint;
select min(start_date), max(end_date)
into :firstdate, :lastdate
from have;
quit;

/* Compute cumulative number of days on drug */

data want(drop=_:);
array _d[&amp;amp;firstdate:&amp;amp;lastdate];
do until(last.id);
  set have;
  by id;
  do _i=start_date to end_date;
    _d[_i]=1;
  end;
  days=sum(of _d[*]);
  output;
end;
label days='Cumulative number of days on any drug';
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sun, 14 Jul 2019 11:31:43 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2019-07-14T11:31:43Z</dc:date>
    <item>
      <title>Calculating Cumulative days with Overlap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/573341#M161853</link>
      <description>&lt;P&gt;I have a data set of patients that start their medication on a certain date and end on a certain date. I would like to calculate the cumulative number of days that they were on medications given that some of them took more than one medication.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    input ID $ medication	start_date :mmddyy10. end_date :mmddyy10.;
    datalines;
A001 1 8/26/2015 9/3/2015
A001 1 9/1/2015 9/17/2017
A002 1 10/2/2015 10/2/2015
A003 1 10/30/2015 10/30/2015
A003 2 11/24/2015 11/24/2015
A004 1 3/17/2016 3/25/2016
A004 2 3/24/2016 3/27/2016
A004 3 3/31/2016 3/31/2016
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;In this case, patient A003 took two medications without overlapping but patient A004 took 3 overlapping medications. How can I create a 5th column that lists the number of cummulative days that they've taken medications? The only idea I have is to create a flag that the patient is taking multiple drugs and that I should subtract the last end date from the first start date, but I wouldn't know how to get rid of the days that the patients didn't take medication in between.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 14 Jul 2019 05:36:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/573341#M161853</guid>
      <dc:creator>serena13lee</dc:creator>
      <dc:date>2019-07-14T05:36:41Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Cumulative days with Overlap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/573352#M161860</link>
      <description>&lt;P&gt;Please see the screenshot, if this is what you wanted. There is column for CumulativeDaysPerMed (number of days on each medicine) and CumulativeDaysPerPatient (number of days for each patient for all medicines). If this is what you wanted then the code follows the image.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Output" style="width: 562px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/31029i1EBC268B7D452CF3/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot from 2019-07-14 13-29-39.png" alt="Output" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;Output&lt;/span&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA WANT (DROP=CurrDay);
	SET have;
	FORMAT start_date mmddyy10. end_date mmddyy10. CurrDay 8. CumulativeDayPerMed 8.;
	BY ID medication;
	RETAIN CumulativeDayPerPatient 0;
	RETAIN CumulativeDayPerMed 0;
	IF LAST.medication AND LAST.ID THEN DO;
		CurrDay = INTCK('day',start_date,end_date);
		CumulativeDayPerMed + CurrDay;
		CumulativeDayPerPatient + CumulativeDayPerMed;
		OUTPUT;
		CumulativeDayPerMed = 0;
		CumulativeDayPerPatient = 0;
	END;
	ELSE DO;
		CurrDay = INTCK('day',start_date,end_date);
		CumulativeDayPerMed + CurrDay;
		CumulativeDayPerPatient + CumulativeDayPerMed;
		OUTPUT;
	END;	
RUN;
	
PROC PRINT DATA=Want;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Please let me know if I was able to read the problem requirement correctly.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;</description>
      <pubDate>Sun, 14 Jul 2019 08:01:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/573352#M161860</guid>
      <dc:creator>koyelghosh</dc:creator>
      <dc:date>2019-07-14T08:01:47Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Cumulative days with Overlap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/573353#M161861</link>
      <description>&lt;P&gt;Hello ,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope so below code will work for your requirement. Kindly let's know how you want to handle missing values.&amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    input ID $ medication	start_date :mmddyy10. end_date :mmddyy10.;
    datalines;
A001 1 8/26/2015 9/3/2015
A001 1 9/1/2015 9/17/2017
A002 1 10/2/2015 10/2/2015
A003 1 10/30/2015 10/30/2015
A003 2 11/24/2015 11/24/2015
A004 1 3/17/2016 3/25/2016
A004 2 3/24/2016 3/27/2016
A004 3 3/31/2016 3/31/2016
;
run;

proc sql;
create table want as
select a.id,a.medication,a.start_date format=date9.,a.end_date format=date9. from have as a , have as b
where (a.id=b.id and a.medication&amp;lt;&amp;gt;b.medication and a.start_date between b.start_date and b.end_date) 
or
(a.id=b.id and a.medication &amp;lt;&amp;gt; b.medication and b.start_date between a.start_date and a.end_date)
or
(a.id=b.id and a.medication &amp;lt;&amp;gt; b.medication and a.end_date  between b.end_date and b.start_date)
or
(a.id=b.id and a.medication &amp;lt;&amp;gt; b.medication and b.end_date  between a.end_date and a.start_date)
or
(a.id=b.id and a.medication &amp;lt;&amp;gt; b.medication and a.end_date=b.end_date and a.start_date=b.start_date)
or
(a.id=b.id and a.medication &amp;lt;&amp;gt; b.medication and b.end_date=a.end_date and b.start_date=a.start_date);
quit;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 14 Jul 2019 08:04:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/573353#M161861</guid>
      <dc:creator>singhsahab</dc:creator>
      <dc:date>2019-07-14T08:04:28Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Cumulative days with Overlap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/573356#M161863</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/250829"&gt;@serena13lee&lt;/a&gt;&amp;nbsp;I am sorry, my previous post was not correct, for two reasons. Please see the code and the output as below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The output of the modified code is below&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Modified output" style="width: 554px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/31030iD8A5B339F9EC3827/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot from 2019-07-14 14-24-58.png" alt="Modified output" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;Modified output&lt;/span&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA WANT (DROP=CurrDay);
	SET have;
	FORMAT start_date mmddyy10. end_date mmddyy10. CurrDay 8. CumulativeDayPerMed 8. CumulativeDayPerPatient 8.;
	BY ID medication;
	RETAIN CumulativeDayPerPatient 0;
	RETAIN CumulativeDayPerMed 0;
	IF LAST.medication AND NOT LAST.ID THEN DO;
		CurrDay = INTCK('day',start_date,end_date)+1;
		CumulativeDayPerMed + CurrDay;
		CumulativeDayPerPatient + CumulativeDayPerMed;
		OUTPUT;
		CumulativeDayPerMed = 0;
	END;
	ELSE IF LAST.medication AND LAST.ID THEN DO;
		CurrDay = INTCK('day',start_date,end_date)+1;
		CumulativeDayPerMed + CurrDay;
		CumulativeDayPerPatient + CumulativeDayPerMed;
		OUTPUT;
		CumulativeDayPerMed = 0;
		CumulativeDayPerPatient = 0;
	END;	
	ELSE DO;
		CurrDay = INTCK('day',start_date,end_date)+1;
		CumulativeDayPerMed + CurrDay;
		CumulativeDayPerPatient + CumulativeDayPerMed;
		OUTPUT;
	END;	
RUN;
	
PROC PRINT DATA=Want;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Hopefully this will suit what you wanted.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 14 Jul 2019 08:56:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/573356#M161863</guid>
      <dc:creator>koyelghosh</dc:creator>
      <dc:date>2019-07-14T08:56:57Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Cumulative days with Overlap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/573359#M161864</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/250829"&gt;@serena13lee&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below code for how I understood your requirement.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.JPG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/31032iA2E14B6C5A10CD6F/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.JPG" alt="Capture.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    input ID $ medication	start_date :mmddyy10. end_date :mmddyy10.;
    format start_date end_date date9.;
    datalines;
A001 1 8/26/2015 9/3/2015
A001 1 9/1/2015 9/17/2017
A002 1 10/2/2015 10/2/2015
A003 1 10/30/2015 10/30/2015
A003 2 11/24/2015 11/24/2015
A004 1 3/17/2016 3/25/2016
A004 2 3/24/2016 3/27/2016
A004 3 3/31/2016 3/31/2016
;

proc sort data=have;
  by id start_date;
run;

data 
  inter(drop=cluster_start_date cluster_end_date)
  clusters(keep=id cluster_:);
  set have;
  by id start_date;
  format cluster_id 16. cluster_start_date cluster_end_date date9.;
  retain cluster_start_date cluster_end_date;

  if first.id then 
    do;
      cluster_id+1;
      cluster_start_date=start_date;
      cluster_end_date=end_date;
    end;
  else
  if start_date&amp;lt;=cluster_end_date then 
    do;
      cluster_end_date=max(cluster_end_date, end_date);
    end;
  else
    do;
      output clusters;
      cluster_id+1;
      cluster_start_date=start_date;
      cluster_end_date=end_date;
    end;
  if last.id then output clusters;

  output inter;
run;
  
data want;
  merge inter clusters;
  by id cluster_id;
  retain days_of_med_in_cluster days_of_med_cum;
  if first.cluster_id then 
    do;
      days_of_med_in_cluster= cluster_end_date-cluster_start_date+1;
      if first.id then days_of_med_cum=days_of_med_in_cluster;
      else days_of_med_cum=sum(days_of_med_cum,days_of_med_in_cluster);
    end;
run;

proc print data=want;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 14 Jul 2019 11:08:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/573359#M161864</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-07-14T11:08:29Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Cumulative days with Overlap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/573362#M161866</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/250829"&gt;@serena13lee&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you just need the&amp;nbsp;cumulative number of days on &lt;EM&gt;any&lt;/EM&gt; drug, you can keep the code simple (at the expense of several seconds of run time for an input dataset with, say, 500,000 observations from 50,000 patients with a mean duration of 10 years on drug):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Determine earliest start date and latest end date */

proc sql noprint;
select min(start_date), max(end_date)
into :firstdate, :lastdate
from have;
quit;

/* Compute cumulative number of days on drug */

data want(drop=_:);
array _d[&amp;amp;firstdate:&amp;amp;lastdate];
do until(last.id);
  set have;
  by id;
  do _i=start_date to end_date;
    _d[_i]=1;
  end;
  days=sum(of _d[*]);
  output;
end;
label days='Cumulative number of days on any drug';
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 14 Jul 2019 11:31:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/573362#M161866</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-07-14T11:31:43Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Cumulative days with Overlap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/573367#M161869</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/250829"&gt;@serena13lee&lt;/a&gt;&amp;nbsp;Very late I realised that may be you are interested in how many days a patient skipped the medicines and how many days a patient took medicines (irrespective of which medicine was taken or skipped). So I thought may be you wanted a table like below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Output table" style="width: 473px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/31033i6BA9516CA75BDB70/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot from 2019-07-14 18-08-57.png" alt="Output table" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;Output table&lt;/span&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So after a bit of workaround and a bit of time investment, I got the code as below. &lt;STRONG&gt;I am sure there is an easier way to accomplish the same task.&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA Want ;
	SET have;
	BY ID medication;
	RETAIN LowerBoundary 0;
	RETAIN UpperBoundary 0;
	RETAIN GapDays 0;
	IF FIRST.ID AND LAST.ID  THEN DO;
		LowerBoundary = MIN(start_date, end_date);
		UpperBoundary = MAX(start_date, end_date);
		Cummulative = INTCK('day',LowerBoundary,UpperBoundary)+1;
		Gapdays = 0;
		OUTPUT;
		LowerBoundary = 0;
		UpperBoundary = 0;
	END;
	ELSE IF FIRST.ID AND NOT LAST.ID THEN DO;
		LowerBoundary = MIN(start_date, end_date);
		UpperBoundary = MAX(start_date, end_date);
		Cummulative = .;
		Gapdays = 0;
		OUTPUT;		
	END;
	ELSE IF NOT FIRST.ID AND LAST.ID THEN DO;
		IF start_date &amp;gt; UpperBoundary THEN GapDays + INTCK('day',UpperBoundary,start_date) - 1;
		IF MIN(start_date, end_date) &amp;lt;= LowerBoundary THEN LowerBoundary = MIN(start_date, end_date);
		IF MAX(start_date, end_date) &amp;gt;= UpperBoundary THEN UpperBoundary = MAX(start_date, end_date);
		Cummulative = INTCK('day',LowerBoundary,UpperBoundary)+1 - GapDays;
		OUTPUT;
		LowerBoundary = 0;
		UpperBoundary = 0;
	END;
	ELSE DO;
		IF start_date &amp;gt; UpperBoundary THEN GapDays + INTCK('day',start_date,UpperBoundary) - 1;
		IF MIN(start_date, end_date) &amp;lt;= LowerBoundary THEN LowerBoundary = MIN(start_date, end_date);
		IF MAX(start_date, end_date) &amp;gt;= UpperBoundary THEN UpperBoundary = MAX(start_date, end_date);
		Cummulative = .;
		OUTPUT;
	END;	
RUN;

PROC REPORT DATA=Want;
	COLUMNS ID GapDays Cummulative;
	DEFINE ID / GROUP "Patient ID" CENTER;
	DEFINE GapDays / ANALYSIS SUM "Number of Days medicine skipped" CENTER;
	DEFINE Cummulative / ANALYSIS SUM "Total Number of days medicine taken" CENTER;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Please let me know, when you can, if this was what you wanted.&lt;/P&gt;</description>
      <pubDate>Sun, 14 Jul 2019 12:42:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/573367#M161869</guid>
      <dc:creator>koyelghosh</dc:creator>
      <dc:date>2019-07-14T12:42:53Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Cumulative days with Overlap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/573394#M161876</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I more than second the approach (which I personally call "paint brushing" and routinely use in similar situations, particularly for calculating what is called "continuous enrollment" in the insurance industry). Some things I'd do differently, though:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;If I understand your intent correctly, the reason you didn't make the array temporary is to let it be&amp;nbsp;auto-reinitialized to missing values at the top of the implied loop each time before the DoW-loop starts iterating for the next BY group. I'd still make it temporary and add CALL MISSING before of after the DoW, which would be the more efficient compared to the non-temp array, the wider the date range should happen to be. In fact, since a temp array can easily swallow millions of items, it can be just made "large enough" without the need for the preliminary data pass determining the array bounds.&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;Instead of _d[_i]=1, _d[_i]+1 can be coded. This way, if the OP wants to count 1 day on N medications as N days, the SUM function will do it; otherwise if it to be counted as 1 regardless of N, the N function will work (in your code, N works just as well as SUM with the same result).&amp;nbsp;&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;In version 9.3 (many orgs still run it), the lowest negative array bound is limited to -128. That is, you can compile an array with the lower bound -129 and lower error-free, yet at run time an array reference with the index lower than -128 will bomb with the error message "ERROR: Array subscript out of range"; this is true for both temp and non-temp arrays. To reliably guard against such an occurrence (i.e. dealing with dates 129 or more days before 01jan1960), the array bounds can be shifted up by &amp;amp;firstdate and the reference _d[_i] can be replaced with _d[_i+&amp;amp;firstdate] accordingly. (Interestingly, this curious 9.3 bug didn't exist before the transition from version 8 to version 9 and was fixed in 9.4.)&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 14 Jul 2019 19:59:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/573394#M161876</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-07-14T19:59:47Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Cumulative days with Overlap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/573397#M161877</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/250829"&gt;@serena13lee&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;You may have noticed that the people who have responded to your inquiry have had to guess what you output is supposed to look like. They wouldn't have had to had you provided a WANT data set to look at. Please afford this courtesy to those kind enough to take their time to help you out and don't assume that your verbal explanation of the result you want to achieve is sufficiently lucid.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 14 Jul 2019 20:05:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/573397#M161877</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-07-14T20:05:29Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Cumulative days with Overlap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/573406#M161881</link>
      <description>&lt;P&gt;Thank you for your solution! It is a very clean method of getting the outcome and I am fitting it to my data to see how it is done in-depthly. Really appreciate your taking the time to look into it as I was struggling to find a way myself.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 14 Jul 2019 23:56:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/573406#M161881</guid>
      <dc:creator>serena13lee</dc:creator>
      <dc:date>2019-07-14T23:56:03Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Cumulative days with Overlap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/573407#M161882</link>
      <description>&lt;P&gt;Thank you for your suggestion. I will keep that in mind. Apologies for the confusion.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 14 Jul 2019 23:58:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/573407#M161882</guid>
      <dc:creator>serena13lee</dc:creator>
      <dc:date>2019-07-14T23:58:21Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Cumulative days with Overlap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/573454#M161901</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21262"&gt;@hashman&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you very much, Paul, for your detailed comments.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;I had tested both types of arrays using an input dataset with about 400,000 obs. and 50,000 patients who were 4,000 days on drug (where the run times were between 5 and 6 seconds). To my surprise, the solution using the temporary array (but not a DoW loop) seemed to be a bit slower (by only 0.4 seconds, so possibly not a significant difference) and because it also required slightly more code I settled for the ordinary array. The possibility of saving the preliminary PROC SQL step is a good point, though. I had included it to avoid any assumptions on &lt;FONT face="courier new,courier"&gt;&amp;amp;firstdate&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;&amp;amp;lastdate&lt;/FONT&gt;.&lt;/LI&gt;
&lt;LI&gt;Yes, I should have used the N function, just because it's the simpler operation, hence likely more efficient. The implied RETAIN of the sum statement (+1) would have required the explicit initialization of the array (for what I assumed was the intended result), which I wanted to avoid.&lt;/LI&gt;
&lt;LI&gt;This is interesting. Thanks for making me aware of this bug.&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Mon, 15 Jul 2019 08:09:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/573454#M161901</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-07-15T08:09:11Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Cumulative days with Overlap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/573545#M161944</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;: Thank you for continuing the interesting conversation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;"&lt;SPAN&gt;The implied RETAIN of the sum statement (+1) would have required the explicit initialization of the array (for what I assumed was the intended result), which I wanted to avoid."&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Very true. Though it can also be avoided by using _d[_i]=sum(_d[_i],1) instead - a usual subterfuge in this kind of situation.&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;On a different note, a temp array can be reinitialized extremely rapidly to &lt;EM&gt;any&lt;/EM&gt; value using CALL FILLMATRIX compiled into a custom call routine via FCMP - with the limitations that (a) the array is 1-based and (b) numeric. I've posted a way of doing it somewhere in this space not a very long time ago (though, as if often happens with pros, given the hint, it would take less time for you to code it yourself than to find my code).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;</description>
      <pubDate>Mon, 15 Jul 2019 15:25:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/573545#M161944</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-07-15T15:25:58Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Cumulative days with Overlap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/573615#M161987</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21262"&gt;@hashman&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;: Thank you for continuing the interesting conversation.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;My pleasure!&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;
&lt;P&gt;&lt;SPAN&gt;... the explicit initialization of the array (...)&amp;nbsp;&lt;/SPAN&gt;can also be avoided by using _d[_i]=sum(_d[_i],1) instead - a usual subterfuge in this kind of situation.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes, sure. I think I've learned this technique about ten years ago from your (and Koen Vyverman's) inspiring 2009 paper "The DOW-Loop Unrolled" and I've used it many times.&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21262"&gt;@hashman&lt;/a&gt;&amp;nbsp;wrote:
&lt;P&gt;On a different note, a temp array can be reinitialized extremely rapidly to &lt;EM&gt;any&lt;/EM&gt; value using CALL FILLMATRIX ....&lt;/P&gt;
&lt;P&gt;I've posted a way of doing it somewhere in this space not a very long time ago ....&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Correct. This was when we discussed my CALL STDIZE approach in&amp;nbsp;&lt;SPAN&gt;&lt;A href="https://communities.sas.com/t5/New-SAS-User/Set-All-Array-Values-to-0/m-p/571066#M12089" target="_blank" rel="noopener"&gt;Set All Array Values to 0&lt;/A&gt;. I'm looking forward to using this and other non-IML matrix functions and CALL routines. Thanks again.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Jul 2019 17:37:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/573615#M161987</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-07-15T17:37:09Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Cumulative days with Overlap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/573750#M162034</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Ah, yeah. In 2008 Koen and I had reckoned that the DoW deserved its own dedicated paper after having been merely a section of "The Magnificent DO" since SESUG 2000; and so we conspired to write a paper and present it using the DATA step debugger rather than PP slides, with Koen at the keyboard and me talking and pointing at the screen. Perhaps it would've been better the other way around since Koen, fluent in 7 languages, speaks English better, not to mention that his Flemish accent is less intrusive than my Ukrainian. At any rate, I'm glad we did it to encapsulate the DoW's functionality in a paper devoted to it and nothing else.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for reminding of the CALL STDIZE thread. Your associative memory is obviously much better than mine. Another thing I must mention is that it was Roger DeAngelis who brought your method to my attention on SAS-L, which in turn spurned me to compare it with the APP and CALL FILLMATRIX. It's pretty incredible how much we can learn from each other here.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Jul 2019 05:26:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/573750#M162034</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-07-16T05:26:52Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Cumulative days with Overlap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/573761#M162037</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21262"&gt;@hashman&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;we ... present it using the DATA step debugger rather than PP slides, with Koen at the keyboard and me talking and pointing at the screen.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Nice idea!&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;
&lt;P&gt;I must mention ... that it was Roger DeAngelis who brought your method to my attention on SAS-L, ...&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I didn't know that. Thanks. A quick Google search ("De Angelis" "CALL STDIZE") brought up this 13 &lt;EM&gt;days&lt;/EM&gt; old GitHub entry:&amp;nbsp;&lt;A href="https://github.com/rogerjdeangelis/utl-set-each-element-of-an-array-to-zero-or-any-constant" target="_blank"&gt;https://github.com/rogerjdeangelis/utl-set-each-element-of-an-array-to-zero-or-any-constant&lt;/A&gt;. I'm amazed how quickly content from this forum is being spread.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Jul 2019 07:20:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/573761#M162037</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-07-16T07:20:03Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Cumulative days with Overlap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/573764#M162038</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;Roger has been doing an amazing job over recent years compiling best solutions of data processing problems from both SAS community and SAS-L and comparing their SAS/R/Python solutions (and their hybrids thereof) on github. A truly wondrous wealth of info there, not to mention that for every problem, Roger goes to great lengths to reformulate it in strikingly lucid terms, replete with HAVE, WANT, and clearly stated processing rules in his own inimitable style.&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Jul 2019 07:48:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/573764#M162038</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-07-16T07:48:37Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Cumulative days with Overlap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/575300#M162705</link>
      <description>&lt;P&gt;Hi! Thank you for your solution. I am currently fitting you code to my data and it looks great. Unfortunately, I have a criteria which I'm not sure how to adapt your code to and was wondering if there might be a simple fix or if I should ask the community. So the criteria I had was performing the cumulative number of days on medication for patients that took medications for &amp;gt;=15 consecutive days . So in the case of patient A004, they took medication for 8 days, then 3 days, then 1 day (11 days total). Since they didn't take medication for &amp;gt;=15 consecutive days, I won't calculate how many days they took medication for. Sorry if this is confusing. Please let me know if you would like an example. Thanks.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Jul 2019 08:28:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/575300#M162705</guid>
      <dc:creator>serena13lee</dc:creator>
      <dc:date>2019-07-22T08:28:02Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Cumulative days with Overlap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/575388#M162749</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/250829"&gt;@serena13lee&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for accepting my solution.&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/250829"&gt;@serena13lee&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;(...) So in the case of patient A004, they took medication for 8 days, then 3 days, then 1 day (11 days total).&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;These counts do not exactly match those produced by my solution: &lt;STRONG&gt;9&lt;/STRONG&gt; days, then &lt;STRONG&gt;4&lt;/STRONG&gt; (of which 2 are overlapping), then 1 day (&lt;STRONG&gt;12&lt;/STRONG&gt; days total). If your counting rule was &lt;FONT face="courier new,courier"&gt;days=end_date-start_date&lt;/FONT&gt;&amp;nbsp;(which is not uncommon) rather than &lt;FONT face="courier new,courier"&gt;days=end_date-start_date&lt;STRONG&gt;+1&lt;/STRONG&gt;&lt;/FONT&gt;, then the last record of your sample data would be inconsistent (as resulting in "0 days").&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That said, my code creates an array &lt;FONT face="courier new,courier"&gt;_d&lt;/FONT&gt; whose elements correspond to the calendar days between the earliest start date and the latest end date. Assuming that the array is populated appropriately, the array element for the i-th date in this time period indicates that the patient took some medication on that day if&amp;nbsp;&lt;FONT face="courier new,courier"&gt;_d[i]=1&lt;/FONT&gt;. Otherwise, the array element contains a numeric missing value:&amp;nbsp;&lt;FONT face="courier new,courier"&gt;_d[i]=.&lt;/FONT&gt;. So, a period of 15 consecutive days on drug means a sequence of 15 consecutive 1s somewhere in the array. You may want to create a flag variable, say, &lt;FONT face="courier new,courier"&gt;consec15d&lt;/FONT&gt; which is set to 1 if such a 15-day sequence exists and which is set to 0 otherwise. To create this flag variable, insert the definition between &lt;FONT face="courier new,courier"&gt;days=...;&lt;/FONT&gt; and the &lt;FONT face="courier new,courier"&gt;output&lt;/FONT&gt; statement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if ~consec15d then consec15d=find(cats(of _d[*]),repeat('1',14))&amp;gt;0;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(Note that&amp;nbsp;&lt;FONT face="courier new,courier"&gt;repeat('1',&lt;STRONG&gt;14&lt;/STRONG&gt;)='111111111111111'&lt;/FONT&gt; with &lt;STRONG&gt;15&lt;/STRONG&gt; digits.) I would assign a label to this variable such as&amp;nbsp;&lt;FONT face="courier new,courier"&gt;'0-1 flag for 15 consecutive days on drug'&lt;/FONT&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The flag will be set to 1 in the first record where the 15-days condition is met and then left unchanged (due to the IF condition, which I've&amp;nbsp;included for performance reasons) because the maximum number of consecutive days on drug can never decrease within an ID, assuming that the records are sorted chronologically. Since CATS creates a character string of the length&amp;nbsp;&lt;FONT face="courier new,courier"&gt;&amp;amp;lastdate-&amp;amp;firstdate+1&lt;/FONT&gt;, I would be hesitant to apply this technique if that length exceeded 32767 characters, but even a time span of &lt;EM&gt;89 years&lt;/EM&gt; wouldn't reach this value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I leave it to you to alter the value of variable DAYS (if necessary) in cases where CONSEC15D=0.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Jul 2019 14:36:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/575388#M162749</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-07-22T14:36:24Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Cumulative days with Overlap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/576418#M163157</link>
      <description>&lt;P&gt;Thank you so much for your quick response. Your explanation was super clear and I have a better idea of what the code is doing. You are right that my days = end_date - start_date +1. Apologies for the confusion. Since I am struggling a bit to add bits to the code I have created a new query so I don't ask too many follow up questions and waste your time (&lt;A href="https://communities.sas.com/t5/SAS-Programming/Flagging-Overlapping-Dates-that-contribute-to-gt-15-Days/m-p/576417#M163156" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/Flagging-Overlapping-Dates-that-contribute-to-gt-15-Days/m-p/576417#M163156&lt;/A&gt;)&amp;nbsp;&amp;nbsp;But thank you again. You've been incredibly helpful!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jul 2019 01:02:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-Cumulative-days-with-Overlap/m-p/576418#M163157</guid>
      <dc:creator>serena13lee</dc:creator>
      <dc:date>2019-07-25T01:02:44Z</dc:date>
    </item>
  </channel>
</rss>

