<?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: Summing overlapping dates and doses in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Summing-overlapping-dates-and-doses/m-p/556243#M154956</link>
    <description>&lt;P&gt;I don't have SAS in front of me over the weekend, so I can only describe this approach in words and untested code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1) Filter out those records where StartDate to EndDate is &amp;lt; 7 days.&lt;/P&gt;&lt;P&gt;2) Use a data step to transpose the data, with one record per day.&amp;nbsp; Example code (untested):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data transposed;
   set have;
   where datdif(StartDate,EndDate,'act/act') &amp;gt;= 7;
   do date=StartDate to EndDate;
      output;
   end;
   drop i;&lt;BR /&gt;   format date date9.;
run;
   &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;3) Use first. to keep only one record with a given date:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=transposed out=sorted;
   by patient date drug;
run;

data no_overlaps;
   set sorted;
   by patient date drug;
   if first.date;
run;

* then count the number of rows to get the number of days minus overlapping&amp;nbsp;days&amp;nbsp;;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 05 May 2019 21:20:50 GMT</pubDate>
    <dc:creator>ScottBass</dc:creator>
    <dc:date>2019-05-05T21:20:50Z</dc:date>
    <item>
      <title>Summing overlapping dates and doses</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summing-overlapping-dates-and-doses/m-p/556223#M154944</link>
      <description>&lt;P&gt;I have the following data set that I would like to melt and reshape by aggregating 1) the total number of days on drugs where it was taken &amp;gt;=7 consecutive days 2) the drug amount they took.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Source data:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;Patient     Drug      StartDate        EndDate  amountperday   unit                                 
A             E      2016-02-20     2016-02-28       1         mg                                                        
A             G      2016-03-20     2016-03-27       2         mg
A             F      2016-02-20     2016-02-21       7         mg  
A             E      2016-02-20                      4         mg 
B             E      2016-01-05     2016-01-24       3         mg
B             G      2016-01-07     2016-01-14       5         mg &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Result:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;Patient     Drug     totamount unit   Days  StartDate   EndDate                               
A           EG       22        mg      15    2016-02-20  2016-03-27                                                
B           EG       95        mg      27    2016-01-05  2016-01-24&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;For A the 22 was calculated 1(8days) + 2(7days).&lt;BR /&gt;For A the 15 was calculated as 8 days + 7 days.&lt;BR /&gt;For B the 95 was calculated as 3(20days) + 5(7days).&lt;BR /&gt;For B the 27 was calculated as 20 days + 14 days - 7 overlapping days.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My attempt at this would be the following:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data calculation;&amp;nbsp;&lt;/P&gt;&lt;P&gt;set mydata;&amp;nbsp;&lt;/P&gt;&lt;P&gt;days=datdif(startdate, enddate, 'act/act');&lt;/P&gt;&lt;P&gt;[I would have to mark the overlapping days but I'm not sure how I would do that and then add a by statement]&lt;/P&gt;&lt;P&gt;if days &amp;gt;=7 then dose = amountperday*days;&lt;/P&gt;&lt;P&gt;else = . ;&amp;nbsp;&lt;/P&gt;&lt;P&gt;run;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please let me know how I can get my desired outcome. It does not have to use my attempt.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 05 May 2019 00:09:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summing-overlapping-dates-and-doses/m-p/556223#M154944</guid>
      <dc:creator>serena13lee</dc:creator>
      <dc:date>2019-05-05T00:09:15Z</dc:date>
    </item>
    <item>
      <title>Re: Summing overlapping dates and doses</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summing-overlapping-dates-and-doses/m-p/556243#M154956</link>
      <description>&lt;P&gt;I don't have SAS in front of me over the weekend, so I can only describe this approach in words and untested code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1) Filter out those records where StartDate to EndDate is &amp;lt; 7 days.&lt;/P&gt;&lt;P&gt;2) Use a data step to transpose the data, with one record per day.&amp;nbsp; Example code (untested):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data transposed;
   set have;
   where datdif(StartDate,EndDate,'act/act') &amp;gt;= 7;
   do date=StartDate to EndDate;
      output;
   end;
   drop i;&lt;BR /&gt;   format date date9.;
run;
   &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;3) Use first. to keep only one record with a given date:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=transposed out=sorted;
   by patient date drug;
run;

data no_overlaps;
   set sorted;
   by patient date drug;
   if first.date;
run;

* then count the number of rows to get the number of days minus overlapping&amp;nbsp;days&amp;nbsp;;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 05 May 2019 21:20:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summing-overlapping-dates-and-doses/m-p/556243#M154956</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2019-05-05T21:20:50Z</dc:date>
    </item>
    <item>
      <title>Re: Summing overlapping dates and doses</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summing-overlapping-dates-and-doses/m-p/556257#M154958</link>
      <description>&lt;P&gt;Honestly, I can't understand your question completely .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Patient   $  Drug   $   StartDate : yymmdd10.       EndDate : yymmdd10. amountperday   unit  $;
format StartDate  EndDate : yymmdd10.;
cards; 
A             E      2016-02-20     2016-02-28       1         mg                                                        
A             G      2016-03-20     2016-03-27       2         mg
A             F      2016-02-20     2016-02-21       7         mg  
A             E      2016-02-20        .              4         mg 
B             E      2016-01-05     2016-01-24       3         mg
B             G      2016-01-07     2016-01-14       5         mg 
;
run;
data temp;
 set have;
 if enddate-startdate&amp;gt;=7;
run;
data want;
   length new_drug $ 100;
   totalamount=0;
   days=0;
 do until(last.patient);
   set temp;
   by patient;
   if first.patient then new_startdate=startdate;
   new_drug=cats(new_drug,drug);
   totalamount+	(enddate-startdate)*amountperday;
   days+(enddate-startdate);
 end;
 new_enddate=enddate;
 format new_startdate new_enddate yymmdd10.;
 keep new_: patient unit totalamount days;
 run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 05 May 2019 12:58:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summing-overlapping-dates-and-doses/m-p/556257#M154958</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-05-05T12:58:03Z</dc:date>
    </item>
  </channel>
</rss>

