<?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: Sum dates intervals based on ID and condition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Sum-dates-intervals-based-on-ID-and-condition/m-p/829312#M327654</link>
    <description>&lt;P&gt;Try this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID :$20. (Start End)(:date9.) Label $;
  format Start End date9.;
cards;
0001 01JAN1998 18JAN1998 A 
0001 20JAN1998 22JAN1998 A 
0001 02FEB1998 02FEB1998 A 
0002 06FEB1998 08FEB1998 B 
0002 03MAR2000 15MAR2000 A 
0002 16MAR2000 18MAR2000 A 
0003 01MAY2000 18MAY2000 C 
0003 19MAY2000 22MAY2000 C 
;

data temp(drop = s);
   set have;
   by ID;
   set have(firstobs = 2 keep = start rename = start = s) have(obs=1 drop=_all_);
   if last.ID then s = .;
   n = s - end;
run;

data want(drop = s _start n);
   do _N_ = 1 by 1 until (n &amp;gt; 2 | last.ID);
      set temp;
      by ID;
      if _N_ = 1 then _start = Start;
   end;

   Start = ifn(_start = ., Start, _start);
   days = End - Start + 1;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Result:&lt;/STRONG&gt;&lt;/U&gt;&lt;U&gt;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;ID    Start      End        Label  days
0001  01JAN1998  22JAN1998  A      22
0001  02FEB1998  02FEB1998  A      1
0002  06FEB1998  08FEB1998  B      3
0002  03MAR2000  18MAR2000  A      16
0003  01MAY2000  22MAY2000  C      22  &amp;nbsp;&amp;nbsp;&lt;/PRE&gt;</description>
    <pubDate>Fri, 19 Aug 2022 09:04:01 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2022-08-19T09:04:01Z</dc:date>
    <item>
      <title>Sum dates intervals based on ID and condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-dates-intervals-based-on-ID-and-condition/m-p/829309#M327652</link>
      <description>&lt;P&gt;Hi guys,&amp;nbsp;&lt;/P&gt;&lt;P&gt;suppose to have the following:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;data have;
  input ID :$20. Start :ddmmyy. End :ddmmyy. Label :$20.;
  format start end yymmdd10.;
cards;&lt;BR /&gt;0001&amp;nbsp;01JAN1998 18JAN1998&amp;nbsp;A&lt;BR /&gt;0001 20JAN1998 22JAN1998 A
0001 02FEB1998 02FEB1998 A
0002 06FEB1998 08FEB1998 B
0002 03MAR2000 15MAR2000 A
0002 16MAR2000 18MAR2000 A&lt;/CODE&gt;&lt;BR /&gt;&lt;CODE&gt;0003 01MAY2000 18MAY2000 C&lt;/CODE&gt;&lt;BR /&gt;0003 19MAY2000 22MAY2000 C&lt;BR /&gt;..... ....................&lt;BR /&gt;;&lt;BR /&gt;&lt;BR /&gt;I need the following:&lt;BR /&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;ID Start End Label Days&lt;BR /&gt;&lt;CODE&gt;0001 01JAN1998 22JAN1998  A      22&lt;BR /&gt;0001 02FEB1998 02FEB1998  A       1
0002 06FEB1998 08FEB1998  B       2
0002 03MAR2000 18MAR2000  A      16&lt;BR /&gt;0003 01MAY2000 22MAY2000  C      22&lt;BR /&gt;..................................&lt;BR /&gt;;&lt;BR /&gt;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Meaning, for each ID and for each condition summarise periods (update the end date and calculate the sum of the days) only if the periods differ of 1 day or they are consecutive like 18-20JAN and 15-16MAR, otherwise do nothing and only count the days in the interval.&lt;/P&gt;&lt;P&gt;Can anyone help me please? I tried to remove the cases where I have not to sum and then merge with the records where I have to sum but SAS stops running. I have 68000 records totally.&lt;/P&gt;&lt;P&gt;Thank you in advance&lt;CODE&gt;&lt;BR /&gt;&lt;/CODE&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Aug 2022 06:35:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-dates-intervals-based-on-ID-and-condition/m-p/829309#M327652</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2022-08-19T06:35:31Z</dc:date>
    </item>
    <item>
      <title>Re: Sum dates intervals based on ID and condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-dates-intervals-based-on-ID-and-condition/m-p/829312#M327654</link>
      <description>&lt;P&gt;Try this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID :$20. (Start End)(:date9.) Label $;
  format Start End date9.;
cards;
0001 01JAN1998 18JAN1998 A 
0001 20JAN1998 22JAN1998 A 
0001 02FEB1998 02FEB1998 A 
0002 06FEB1998 08FEB1998 B 
0002 03MAR2000 15MAR2000 A 
0002 16MAR2000 18MAR2000 A 
0003 01MAY2000 18MAY2000 C 
0003 19MAY2000 22MAY2000 C 
;

data temp(drop = s);
   set have;
   by ID;
   set have(firstobs = 2 keep = start rename = start = s) have(obs=1 drop=_all_);
   if last.ID then s = .;
   n = s - end;
run;

data want(drop = s _start n);
   do _N_ = 1 by 1 until (n &amp;gt; 2 | last.ID);
      set temp;
      by ID;
      if _N_ = 1 then _start = Start;
   end;

   Start = ifn(_start = ., Start, _start);
   days = End - Start + 1;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Result:&lt;/STRONG&gt;&lt;/U&gt;&lt;U&gt;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;ID    Start      End        Label  days
0001  01JAN1998  22JAN1998  A      22
0001  02FEB1998  02FEB1998  A      1
0002  06FEB1998  08FEB1998  B      3
0002  03MAR2000  18MAR2000  A      16
0003  01MAY2000  22MAY2000  C      22  &amp;nbsp;&amp;nbsp;&lt;/PRE&gt;</description>
      <pubDate>Fri, 19 Aug 2022 09:04:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-dates-intervals-based-on-ID-and-condition/m-p/829312#M327654</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-08-19T09:04:01Z</dc:date>
    </item>
    <item>
      <title>Re: Sum dates intervals based on ID and condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-dates-intervals-based-on-ID-and-condition/m-p/829323#M327657</link>
      <description>&lt;P&gt;Do a "look-ahead":&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID :$20. Start :date9. End :date9. Label :$20.;
  format start end yymmdd10.;
cards;
0001 01JAN1998 18JAN1998 A
0001 20JAN1998 22JAN1998 A
0001 02FEB1998 02FEB1998 A
0002 06FEB1998 08FEB1998 B
0002 03MAR2000 15MAR2000 A
0002 16MAR2000 18MAR2000 A
0003 01MAY2000 18MAY2000 C
0003 19MAY2000 22MAY2000 C
;

data want;
merge
  have
  have (firstobs=2 drop=end rename=(id=_id start=_start label=_label))
;
retain __start;
if _n_ = 1 then __start = start;
if
  id = _id and label = _label and _start - end gt 2
  or id ne _id
  or label ne _label
then do;
  start = __start;
  days = end - start + 1;
  output;
  __start = _start;
end;
drop _:;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;ID	Start	End	Label	days
0001	1998-01-01	1998-01-22	A	22
0001	1998-02-02	1998-02-02	A	1
0002	1998-02-06	1998-02-08	B	3
0002	2000-03-03	2000-03-18	A	16
0003	2000-05-01	2000-05-22	C	22
&lt;/PRE&gt;
&lt;P&gt;Please test your example data code before posting; I had to change the informats for the dates.&lt;/P&gt;</description>
      <pubDate>Fri, 19 Aug 2022 08:51:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-dates-intervals-based-on-ID-and-condition/m-p/829323#M327657</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-08-19T08:51:36Z</dc:date>
    </item>
    <item>
      <title>Re: Sum dates intervals based on ID and condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-dates-intervals-based-on-ID-and-condition/m-p/829325#M327658</link>
      <description>&lt;P&gt;Thank you dottor Clemmensen! Finally it works. I had only to manage some cases by removing them from the data.set, processing them separately and then I merged all and the result was what I was looking for. Thank you so much.&lt;/P&gt;</description>
      <pubDate>Fri, 19 Aug 2022 09:25:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-dates-intervals-based-on-ID-and-condition/m-p/829325#M327658</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2022-08-19T09:25:42Z</dc:date>
    </item>
    <item>
      <title>Re: Sum dates intervals based on ID and condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-dates-intervals-based-on-ID-and-condition/m-p/829326#M327659</link>
      <description>&lt;P&gt;Anytime &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Aug 2022 09:25:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-dates-intervals-based-on-ID-and-condition/m-p/829326#M327659</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-08-19T09:25:33Z</dc:date>
    </item>
    <item>
      <title>Re: Sum dates intervals based on ID and condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-dates-intervals-based-on-ID-and-condition/m-p/829327#M327660</link>
      <description>Sorry dott. Bremen. I was quite in trouble with timing.</description>
      <pubDate>Fri, 19 Aug 2022 09:26:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-dates-intervals-based-on-ID-and-condition/m-p/829327#M327660</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2022-08-19T09:26:38Z</dc:date>
    </item>
  </channel>
</rss>

