<?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: Combining  continuous date ranges by ID in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Combining-continuous-date-ranges-by-ID/m-p/931797#M366580</link>
    <description>&lt;P&gt;This solution uses PROC SORT and Group processing in the DATA STEP.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=have out=have2;&lt;BR /&gt;by id start_dt end_dt;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have2;&lt;BR /&gt;set have2;&lt;BR /&gt;retain id2 start_dt2;&lt;BR /&gt;by id start_dt end_dt;&lt;/P&gt;
&lt;P&gt;if first.id then&lt;BR /&gt;do;&lt;BR /&gt;id2=id;&lt;BR /&gt;start_dt2=start_dt;&lt;BR /&gt;end;&lt;/P&gt;
&lt;P&gt;if last.id then&lt;BR /&gt;end_dt2=end_dt;&lt;/P&gt;
&lt;P&gt;if last.id;&lt;BR /&gt;format start_dt2 end_dt2 mmddyy10.;&lt;BR /&gt;keep id2 start_dt2 end_dt2;&lt;BR /&gt;run;&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="JOL_0-1718137557230.png" style="width: 479px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/97225iB5B8C46B16036FCD/image-dimensions/479x246?v=v2" width="479" height="246" role="button" title="JOL_0-1718137557230.png" alt="JOL_0-1718137557230.png" /&gt;&lt;/span&gt;&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>Tue, 11 Jun 2024 20:27:06 GMT</pubDate>
    <dc:creator>JOL</dc:creator>
    <dc:date>2024-06-11T20:27:06Z</dc:date>
    <item>
      <title>Combining  continuous date ranges by ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combining-continuous-date-ranges-by-ID/m-p/931709#M366536</link>
      <description>&lt;P&gt;Here is a small sample of a data set that looks like this:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;DATA HAVE;
INPUT ID $10. START_DT :MMDDYY10. END_DT :MMDDYY10.;
format START_DT MMDDYY10. END_DT MMDDYY10.;
datalines;
1004064609 04/01/2023 12/31/2023
1004064609 01/01/2024 12/31/3999
1004064623 04/01/2023 12/31/2023
1004064623 01/01/2024 12/31/3999
1004064703 03/06/2024 03/28/2024
1004064703 03/29/2024 12/31/3999
1004064706 04/01/2023 12/31/2023
1004064706 01/01/2024 12/31/3999
1004064724 04/01/2023 06/30/2023
1004064724 07/01/2023 11/30/2023
1004064724 12/01/2023 12/31/2023
1004064724 01/01/2024 12/31/3999
1004064726 04/01/2023 10/31/2023
1004064726 12/05/2023 12/31/2023
1004064726 01/01/2024 05/31/2024
1004064726 06/01/2024 12/31/3999
1004064727 04/01/2023 12/31/2023
1004064727 01/01/2024 12/31/3999
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;For each unique ID I want to collapse continuous date spans whenever possible. For example, for the first ID 1004064609 I would want a single record where START_DT = 04/01/2023 AND END_DT = 12/31/3999. Note that some IDs will have multiple date spans that aren't continuous and those should continue to exist as separate records. What's a solution to do this? Thank you.&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jun 2024 14:45:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combining-continuous-date-ranges-by-ID/m-p/931709#M366536</guid>
      <dc:creator>thelowendHz</dc:creator>
      <dc:date>2024-06-11T14:45:58Z</dc:date>
    </item>
    <item>
      <title>Re: Combining  continuous date ranges by ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combining-continuous-date-ranges-by-ID/m-p/931739#M366544</link>
      <description>&lt;P&gt;I changed your infinity date to 2050 from 3999 (too far).&amp;nbsp; &amp;nbsp; This method expands the ranges and then looks for gaps GT 1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA HAVE;
   INPUT ID $10. START_DT :MMDDYY10. END_DT :MMDDYY10.;
   format START_DT MMDDYY10. END_DT MMDDYY10.;
   datalines;
1004064609 04/01/2023 12/31/2023
1004064609 01/01/2024 12/31/2050
1004064623 04/01/2023 12/31/2023
1004064623 01/01/2024 12/31/2050
1004064703 03/06/2024 03/28/2024
1004064703 03/29/2024 12/31/2050
1004064706 04/01/2023 12/31/2023
1004064706 01/01/2024 12/31/2050
1004064724 04/01/2023 06/30/2023
1004064724 07/01/2023 11/30/2023
1004064724 12/01/2023 12/31/2023
1004064724 01/01/2024 12/31/2050
1004064726 04/01/2023 10/31/2023
1004064726 12/05/2023 12/31/2023
1004064726 01/01/2024 05/31/2024
1004064726 06/01/2024 12/31/2050
1004064727 04/01/2023 12/31/2023
1004064727 01/01/2024 12/31/2050
;
   RUN;

data expand / view=expand;
   set have;
   do date=start_dt to end_dt;
      output;
      end;
   keep id date;
   format date yymmdd10.;
   run;

proc summary data=expand nway;
   by id;
   class date;
   output out=unique(drop=_:);
   run;
data group / view=group;
   set unique;
   by id;
   if first.id then group=0;
   if dif(date) ne 1 then group+1;
   run;
proc summary data=group nway missing;
   by id;
   class group;
   output out=range(drop=_:) min(date)=start max(date)=end range(date)=Range;
   format range: ; 
   run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 350px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/97215i7B4CA0A8F6FC11B5/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jun 2024 15:57:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combining-continuous-date-ranges-by-ID/m-p/931739#M366544</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2024-06-11T15:57:30Z</dc:date>
    </item>
    <item>
      <title>Re: Combining  continuous date ranges by ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combining-continuous-date-ranges-by-ID/m-p/931787#M366573</link>
      <description>&lt;P&gt;If you don't have overlapping intervals then perhaps:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;/* assumes Have is sorted by ID and Start_dt*/
data temp;
   set have;
   by id ;
   retain tstart;
   lend = lag(end_dt);
   if first.id then tstart=start_dt;
   else if intck('day',lend,start_dt)=1 then start_dt=tstart;
   else tstart=start_dt;
   drop tstart lend;
run;
data want;
  set temp;
  by id start_dt;
  if last.start_dt;
run;&lt;/PRE&gt;</description>
      <pubDate>Tue, 11 Jun 2024 19:43:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combining-continuous-date-ranges-by-ID/m-p/931787#M366573</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-06-11T19:43:01Z</dc:date>
    </item>
    <item>
      <title>Re: Combining  continuous date ranges by ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combining-continuous-date-ranges-by-ID/m-p/931797#M366580</link>
      <description>&lt;P&gt;This solution uses PROC SORT and Group processing in the DATA STEP.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=have out=have2;&lt;BR /&gt;by id start_dt end_dt;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have2;&lt;BR /&gt;set have2;&lt;BR /&gt;retain id2 start_dt2;&lt;BR /&gt;by id start_dt end_dt;&lt;/P&gt;
&lt;P&gt;if first.id then&lt;BR /&gt;do;&lt;BR /&gt;id2=id;&lt;BR /&gt;start_dt2=start_dt;&lt;BR /&gt;end;&lt;/P&gt;
&lt;P&gt;if last.id then&lt;BR /&gt;end_dt2=end_dt;&lt;/P&gt;
&lt;P&gt;if last.id;&lt;BR /&gt;format start_dt2 end_dt2 mmddyy10.;&lt;BR /&gt;keep id2 start_dt2 end_dt2;&lt;BR /&gt;run;&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="JOL_0-1718137557230.png" style="width: 479px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/97225iB5B8C46B16036FCD/image-dimensions/479x246?v=v2" width="479" height="246" role="button" title="JOL_0-1718137557230.png" alt="JOL_0-1718137557230.png" /&gt;&lt;/span&gt;&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>Tue, 11 Jun 2024 20:27:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combining-continuous-date-ranges-by-ID/m-p/931797#M366580</guid>
      <dc:creator>JOL</dc:creator>
      <dc:date>2024-06-11T20:27:06Z</dc:date>
    </item>
  </channel>
</rss>

