<?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 How can I retain unique date ranges only after collapsing any overlapping dates within a unique ID? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-retain-unique-date-ranges-only-after-collapsing-any/m-p/893850#M353118</link>
    <description>&lt;P&gt;SAS learner here! I have a dataset with start and stop dates of eligibility; unique IDs in the data can repeat, with multiple start (eligeff) and stop (eligend) dates of eligibility. Within a unique ID, I am trying to 1) collapse any overlapping date ranges or 2) stitch together any date ranges that are a maximum of 1 day apart (i.e. one row has a stop date of 12.31.2019 and the next row has a start date of 01.01.2020). Any eligibility periods 2 days or more apart should remain as they are (i.e. one row has a stop date of 12.31.2019 and the next start date is 01.02.2020). Here is the code I've written so far:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc sort data=amlgroup.mbr_0;
by patid eligeff eligend;
run;

data amlgroup.mbr_1;
set amlgroup.mbr_0;
by patid eligeff eligend;
retain maxenr_dt minenr_dt;
if first.patid then do;
	minenr_dt = eligeff; maxenr_dt = eligend;
	end;
else do;
if eligend &amp;lt;=maxenr_dt then delete;
if . &amp;lt; eligeff - maxenr_dt &amp;lt;= 1 and maxenr_dt &amp;lt; eligend then maxenr_dt = eligend;
if eligeff - maxenr_dt &amp;gt; 1 then do;
	minenr_dt = eligeff; maxenr_dt = eligend;
	end;
end;
format maxenr_dt minenr_dt yymmdd10.;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The problem I have is when I grab minenr_dt and maxenr_dt for the next phase of my project, as I only want the final, collapsed date ranges (see example of resulting dataset below). I need to drop rows where the date ranges are nested within a larger date range for the same ID (see example, I need to drop the first row for ID 123456), but still be able to keep all non-overlapping date ranges (see example, I don't want to drop either of the rows for ID 789100). Any suggestions for how to change my code? Thank you.&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;ID&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;eligeff&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;eligend&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;minenr_dt&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;maxenr_dt&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;123456&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;01.01.2015&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;09.30.2018&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;01.01.2015&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;09.30.2018&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;123456&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;10.01.2018&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;03.25.2022&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;01.01.2015&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;03.25.2020&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;789100&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;02.01.2017&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;12.31.2018&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;02.01.2017&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;12.31.2018&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;789100&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;01.01.2020&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;06.30.2022&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;01.01.2020&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;06.30.2022&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
    <pubDate>Tue, 12 Sep 2023 16:54:34 GMT</pubDate>
    <dc:creator>dominiquec</dc:creator>
    <dc:date>2023-09-12T16:54:34Z</dc:date>
    <item>
      <title>How can I retain unique date ranges only after collapsing any overlapping dates within a unique ID?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-retain-unique-date-ranges-only-after-collapsing-any/m-p/893850#M353118</link>
      <description>&lt;P&gt;SAS learner here! I have a dataset with start and stop dates of eligibility; unique IDs in the data can repeat, with multiple start (eligeff) and stop (eligend) dates of eligibility. Within a unique ID, I am trying to 1) collapse any overlapping date ranges or 2) stitch together any date ranges that are a maximum of 1 day apart (i.e. one row has a stop date of 12.31.2019 and the next row has a start date of 01.01.2020). Any eligibility periods 2 days or more apart should remain as they are (i.e. one row has a stop date of 12.31.2019 and the next start date is 01.02.2020). Here is the code I've written so far:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc sort data=amlgroup.mbr_0;
by patid eligeff eligend;
run;

data amlgroup.mbr_1;
set amlgroup.mbr_0;
by patid eligeff eligend;
retain maxenr_dt minenr_dt;
if first.patid then do;
	minenr_dt = eligeff; maxenr_dt = eligend;
	end;
else do;
if eligend &amp;lt;=maxenr_dt then delete;
if . &amp;lt; eligeff - maxenr_dt &amp;lt;= 1 and maxenr_dt &amp;lt; eligend then maxenr_dt = eligend;
if eligeff - maxenr_dt &amp;gt; 1 then do;
	minenr_dt = eligeff; maxenr_dt = eligend;
	end;
end;
format maxenr_dt minenr_dt yymmdd10.;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The problem I have is when I grab minenr_dt and maxenr_dt for the next phase of my project, as I only want the final, collapsed date ranges (see example of resulting dataset below). I need to drop rows where the date ranges are nested within a larger date range for the same ID (see example, I need to drop the first row for ID 123456), but still be able to keep all non-overlapping date ranges (see example, I don't want to drop either of the rows for ID 789100). Any suggestions for how to change my code? Thank you.&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;ID&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;eligeff&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;eligend&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;minenr_dt&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;maxenr_dt&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;123456&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;01.01.2015&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;09.30.2018&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;01.01.2015&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;09.30.2018&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;123456&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;10.01.2018&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;03.25.2022&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;01.01.2015&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;03.25.2020&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;789100&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;02.01.2017&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;12.31.2018&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;02.01.2017&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;12.31.2018&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;789100&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;01.01.2020&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;06.30.2022&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;01.01.2020&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;06.30.2022&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Tue, 12 Sep 2023 16:54:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-retain-unique-date-ranges-only-after-collapsing-any/m-p/893850#M353118</guid>
      <dc:creator>dominiquec</dc:creator>
      <dc:date>2023-09-12T16:54:34Z</dc:date>
    </item>
    <item>
      <title>Re: How can I retain unique date ranges only after collapsing any overlapping dates within a unique</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-retain-unique-date-ranges-only-after-collapsing-any/m-p/893974#M353162</link>
      <description>&lt;P&gt;Using two dataset name parameters (&lt;EM&gt;&lt;STRONG&gt;firstobs=&lt;/STRONG&gt;&lt;/EM&gt;&amp;nbsp; and &lt;EM&gt;&lt;STRONG&gt;rename=&lt;/STRONG&gt;&lt;/EM&gt;&amp;nbsp; (and yes &lt;EM&gt;&lt;STRONG&gt;keep=&lt;/STRONG&gt;&lt;/EM&gt;, so 3 parameters) in a MERGE statement makes this very easy with a sorted dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data splicer_date_ranges (drop=nxt_:);
  do until (nxt_id^=id or nxt_eligeff&amp;gt;sum(eligend,1));
    merge have
          have (firstobs=2 keep=id eligeff rename=(id=nxt_id eligeff=nxt_eligeff));
    minenr_dt=coalesce(minenr_dt,eligeff);
    maxenr_dt=eligend;
  end;
  format minenr_dt maxenr_dt date9. ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit note: corrected the AND to OR in the do until expression, (and changed the inequality test).&lt;/P&gt;</description>
      <pubDate>Wed, 13 Sep 2023 00:36:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-retain-unique-date-ranges-only-after-collapsing-any/m-p/893974#M353162</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-09-13T00:36:21Z</dc:date>
    </item>
    <item>
      <title>Re: How can I retain unique date ranges only after collapsing any overlapping dates within a unique</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-retain-unique-date-ranges-only-after-collapsing-any/m-p/894283#M353269</link>
      <description>Thank you! I was able to apply the code you suggested successfully.</description>
      <pubDate>Thu, 14 Sep 2023 15:09:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-retain-unique-date-ranges-only-after-collapsing-any/m-p/894283#M353269</guid>
      <dc:creator>dominiquec</dc:creator>
      <dc:date>2023-09-14T15:09:08Z</dc:date>
    </item>
  </channel>
</rss>

