<?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: Finding Earliest begin date and latest end date in a non continuous dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Finding-Earliest-begin-date-and-latest-end-date-in-a-non/m-p/878092#M346920</link>
    <description>&lt;P&gt;What if you have more than one obs at earliest start or latest end?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And also, your last obs has start of&amp;nbsp;&lt;SPAN&gt;2022-01-01 and end of 2020-12-31.&amp;nbsp; Doesn't make sense.&amp;nbsp; In fact, that end is an actual tie between the last two obs.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Finally, is your data sorted by start?&amp;nbsp; Your sample is.&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 29 May 2023 20:18:53 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2023-05-29T20:18:53Z</dc:date>
    <item>
      <title>Finding Earliest begin date and latest end date in a non continuous dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-Earliest-begin-date-and-latest-end-date-in-a-non/m-p/878089#M346918</link>
      <description>I have a dataset like this:&lt;BR /&gt;ID Start date End date&lt;BR /&gt;A 2015-01-01 2015-12-31&lt;BR /&gt;A 2016-01-01 2016-12-31&lt;BR /&gt;A 2017-01-03. 2017-12-31&lt;BR /&gt;B 2020-01-01 2020-12-31&lt;BR /&gt;B. 2022-01-01 2020-12-31&lt;BR /&gt;&lt;BR /&gt;I want my final dataset to be&lt;BR /&gt;ID Start date End date&lt;BR /&gt;A 2015-01-01 2016-12-31&lt;BR /&gt;B 2020-01-01 2020-12-31&lt;BR /&gt;&lt;BR /&gt;It should be the earliest begin date and latest end date from the continuous coverage. If there is a break in the next begin date then it should not consider the rows after that for that particular ID.</description>
      <pubDate>Mon, 29 May 2023 18:59:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-Earliest-begin-date-and-latest-end-date-in-a-non/m-p/878089#M346918</guid>
      <dc:creator>sneha-25</dc:creator>
      <dc:date>2023-05-29T18:59:26Z</dc:date>
    </item>
    <item>
      <title>Re: Finding Earliest begin date and latest end date in a non continuous dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-Earliest-begin-date-and-latest-end-date-in-a-non/m-p/878092#M346920</link>
      <description>&lt;P&gt;What if you have more than one obs at earliest start or latest end?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And also, your last obs has start of&amp;nbsp;&lt;SPAN&gt;2022-01-01 and end of 2020-12-31.&amp;nbsp; Doesn't make sense.&amp;nbsp; In fact, that end is an actual tie between the last two obs.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Finally, is your data sorted by start?&amp;nbsp; Your sample is.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 29 May 2023 20:18:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-Earliest-begin-date-and-latest-end-date-in-a-non/m-p/878092#M346920</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-05-29T20:18:53Z</dc:date>
    </item>
    <item>
      <title>Re: Finding Earliest begin date and latest end date in a non continuous dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-Earliest-begin-date-and-latest-end-date-in-a-non/m-p/878148#M346942</link>
      <description>&lt;P&gt;You could just use normal code for collapsing to continuous periods.&lt;/P&gt;
&lt;P&gt;So perhaps something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID $ (start end) (:yymmdd.);
  format start end yymmdd10.;
cards;
A 2015-01-01 2015-12-31
A 2016-01-01 2016-12-31
A 2017-01-03 2017-12-31
B 2020-01-01 2020-12-31
B 2022-01-01 2022-12-31
;

data want;
 set have ;
 by id start end;
 if first.id then do;
    period=0;
    lagend = start-2;
 end;
 retain period lagend min max;
 format lagend min max yymmdd10.;
 if 1&amp;lt;(start-lagend) then do;
   if period then output;
   period+1;
   min=start;
   max=end;
 end;
 max=max(max,end);
 if last.id then output;
 lagend=end;
 drop start end lagend;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;OBS    ID    period           min           max

 1     A        1      2015-01-01    2016-12-31
 2     A        2      2017-01-03    2017-12-31
 3     B        1      2020-01-01    2020-12-31
 4     B        2      2022-01-01    2022-12-31

&lt;/PRE&gt;
&lt;P&gt;And just modify it to only OUTPUT when PERIOD is 1.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
 set have ;
 by id start end;
 if first.id then do;
    period=0;
    lagend = start-2;
 end;
 retain period lagend min max;
 format lagend min max yymmdd10.;
 if 1&amp;lt;(start-lagend) then do;
   if period=1 then output;
   period+1;
   min=start;
   max=end;
 end;
 max=max(max,end);
 if last.id and period=1 then output;
 lagend=end;
 drop start end lagend;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;OBS    ID    period           min           max

 1     A        1      2015-01-01    2016-12-31
 2     B        1      2020-01-01    2020-12-31

&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 May 2023 03:49:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-Earliest-begin-date-and-latest-end-date-in-a-non/m-p/878148#M346942</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-05-30T03:49:24Z</dc:date>
    </item>
    <item>
      <title>Re: Finding Earliest begin date and latest end date in a non continuous dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-Earliest-begin-date-and-latest-end-date-in-a-non/m-p/878211#M346964</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID $ Startdate :yymmdd12. Enddate :yymmdd12.;
format Startdate  Enddate :yymmdd10.;
cards;
A 2015-01-01 2015-12-31
A 2016-01-01 2016-12-31
A 2017-01-03 2017-12-31
B 2020-01-01 2020-12-31
B 2022-01-01 2020-12-31
;

data temp;
 set have;
 do date=Startdate to  Enddate;
   output;
 end;
format date yymmdd10.;
keep id date;
run;
proc sort data=temp out=temp2 nodupkey;
by id date;
run;
data temp3;
 set temp2;
 by id;
 if first.id then group=0;
 if first.id or dif(date) ne 1 then group+1;

 if group=1;
run;
proc summary data=temp3;
by id;
var date;
output out=want(drop=_:) min=Startdate max=Enddate;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 30 May 2023 12:39:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-Earliest-begin-date-and-latest-end-date-in-a-non/m-p/878211#M346964</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-05-30T12:39:13Z</dc:date>
    </item>
  </channel>
</rss>

