<?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: Collapsing table based on conditions in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Collapsing-table-based-on-conditions/m-p/523588#M142256</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input ID	(DateFrom 	DateThru) (:mmddyy10.);
format datefrom datethru mmddyy10.;
cards;
123	10/1/2004	5/25/2006
123	5/26/2006	8/28/2007
123	8/29/2007	3/17/2008
123	3/18/2008	10/14/2008
123	12/7/2015	12/31/2015
123	2/12/2016	12/31/9999
221	1/1/2005	5/26/2008
221	11/11/2011	8/1/2015
221	8/2/2015	10/12/2015
221	10/13/2015	6/30/2018
221	7/1/2018	12/31/9999
;
data temp;
set have;
by id;
if first.id then f=1; 
If not first.id and DateFrom ne lag(DateThru)+1 then f+1;
run;

proc sql;
create table want as
select ID, min(datefrom) as Datefrom format=mmddyy10., max(DateThru) as Datethru format=mmddyy10.
from (select * from temp group by id having f=max(f))
group by id, f ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 26 Dec 2018 21:47:25 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2018-12-26T21:47:25Z</dc:date>
    <item>
      <title>Collapsing table based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Collapsing-table-based-on-conditions/m-p/523581#M142253</link>
      <description>&lt;P class="lia-message-dates lia-message-post-date lia-component-post-date-last-edited"&gt;&lt;SPAN class="DateTime lia-message-posted-on lia-component-common-widget-date"&gt;&amp;nbsp;Hi there,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV class="lia-quilt-column lia-quilt-column-20 lia-quilt-column-right lia-quilt-column-main-right"&gt;&lt;DIV class="lia-quilt-column-alley lia-quilt-column-alley-right"&gt;&lt;DIV class="lia-message-body"&gt;&lt;DIV class="lia-message-body-content"&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm struggling to come up with a solution to this problem and could use some input.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For each ID, I have a variable amount of date ranges. I am trying to create a single row for each ID that shows 1) the most recent end date (DateThru); and 2) the earliest start date (DateFrom)&amp;nbsp;without a break greater than&amp;nbsp;1 day (using the most recent DateThru as an anchor).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is an idea of what my data looks like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;DateFrom&lt;/TD&gt;&lt;TD&gt;DateThru&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;123&lt;/TD&gt;&lt;TD&gt;10/1/2004&lt;/TD&gt;&lt;TD&gt;5/25/2006&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;123&lt;/TD&gt;&lt;TD&gt;5/26/2006&lt;/TD&gt;&lt;TD&gt;8/28/2007&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;123&lt;/TD&gt;&lt;TD&gt;8/29/2007&lt;/TD&gt;&lt;TD&gt;3/17/2008&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;123&lt;/TD&gt;&lt;TD&gt;3/18/2008&lt;/TD&gt;&lt;TD&gt;10/14/2008&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;123&lt;/TD&gt;&lt;TD&gt;12/7/2015&lt;/TD&gt;&lt;TD&gt;12/31/2015&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;123&lt;/TD&gt;&lt;TD&gt;2/12/2016&lt;/TD&gt;&lt;TD&gt;12/31/9999&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In this example I would want to only output the last row. The second newest date range ended 12/31/15, so it ended greater than 1 day before 2/12/16. All other records are immaterial.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is another example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;DateFrom&lt;/TD&gt;&lt;TD&gt;DateThru&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;221&lt;/TD&gt;&lt;TD&gt;1/1/2005&lt;/TD&gt;&lt;TD&gt;5/26/2008&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;221&lt;/TD&gt;&lt;TD&gt;11/11/2011&lt;/TD&gt;&lt;TD&gt;8/1/2015&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;221&lt;/TD&gt;&lt;TD&gt;8/2/2015&lt;/TD&gt;&lt;TD&gt;10/12/2015&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;221&lt;/TD&gt;&lt;TD&gt;10/13/2015&lt;/TD&gt;&lt;TD&gt;6/30/2018&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;221&lt;/TD&gt;&lt;TD&gt;7/1/2018&lt;/TD&gt;&lt;TD&gt;12/31/9999&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In this example I would want to see a single row for ID=221 that shows DateFrom = 11/11/2011 and DateThru = 12/31/9999. All but the first row show continuous date ranges with gaps of 1 day only.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any thoughts?&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Wed, 26 Dec 2018 20:18:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Collapsing-table-based-on-conditions/m-p/523581#M142253</guid>
      <dc:creator>rlafond</dc:creator>
      <dc:date>2018-12-26T20:18:09Z</dc:date>
    </item>
    <item>
      <title>Re: Collapsing table based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Collapsing-table-based-on-conditions/m-p/523585#M142255</link>
      <description>&lt;P&gt;What do you mean by "a break greater than one day"?&amp;nbsp; Does that mean the DATEFROM in a record must start on the day immediately following DATETHRU in the preceding record?&amp;nbsp; Or does it mean that DATEFROM can be the 2nd day after the prior DATETHRU, leaving exactly 1 day uncovered?&amp;nbsp; I.e.&amp;nbsp; what's a "break"?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming you want no uncovered days, then you want to generate continuous time spans constructed from a sequence of records, and&amp;nbsp;keep only the last of those time spans.&amp;nbsp; I assume your data are sorted by ID DATEFROM, and that DATETHRU is always greater than or equal to DATEFROM:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id datefrom :mmddyy10. datethru :mmddyy10.;
  format date: date9.;
datalines;
123 10/1/2004 5/25/2006
123 5/26/2006 8/28/2007
123 8/29/2007 3/17/2008
123 3/18/2008 10/14/2008
123 12/7/2015 12/31/2015
123 2/12/2016 12/31/9999
221 1/1/2005 5/26/2008 
221 11/11/2011 8/1/2015 
221 8/2/2015 10/12/2015 
221 10/13/2015 6/30/2018 
221 7/1/2018 12/31/9999 
run;

data want (drop=_:);
  set have (keep=id);
  by id;
  merge have 
        have (firstobs=2 keep=datefrom rename=(datefrom=_nxt_from));

  if first.id=1 or datefrom-1&amp;gt;lag(datethru) then _datefrom=datefrom;
  else datefrom=_datefrom;

  if last.id=1;
  retain _datefrom;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The task here is to detect&amp;nbsp;whenever the incoming record is either the beginning of an id, or is more than 1 day after the preceding record.&amp;nbsp; In that case store the current DATEFROM value into a retained variable (_DATEFROM), otherwise assign the retained _DATEFROM value into the current DATEFROM variable.&amp;nbsp;&amp;nbsp;&amp;nbsp;No modifications are needed to DATETHRU.&amp;nbsp; Then just keep the last incoming record, which would have&amp;nbsp;its DATEFROM value properly updated, if needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note if you actually will permit uncovered single days in successive records, just change&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;if first.id=1 or datefrom-1&amp;gt;lag(datethru) then ...&lt;/PRE&gt;
&lt;P&gt;to&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;if first.id=1 or datefrom-2&amp;gt;lag(datethru) then ...&lt;/PRE&gt;</description>
      <pubDate>Wed, 26 Dec 2018 21:01:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Collapsing-table-based-on-conditions/m-p/523585#M142255</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-12-26T21:01:04Z</dc:date>
    </item>
    <item>
      <title>Re: Collapsing table based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Collapsing-table-based-on-conditions/m-p/523588#M142256</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input ID	(DateFrom 	DateThru) (:mmddyy10.);
format datefrom datethru mmddyy10.;
cards;
123	10/1/2004	5/25/2006
123	5/26/2006	8/28/2007
123	8/29/2007	3/17/2008
123	3/18/2008	10/14/2008
123	12/7/2015	12/31/2015
123	2/12/2016	12/31/9999
221	1/1/2005	5/26/2008
221	11/11/2011	8/1/2015
221	8/2/2015	10/12/2015
221	10/13/2015	6/30/2018
221	7/1/2018	12/31/9999
;
data temp;
set have;
by id;
if first.id then f=1; 
If not first.id and DateFrom ne lag(DateThru)+1 then f+1;
run;

proc sql;
create table want as
select ID, min(datefrom) as Datefrom format=mmddyy10., max(DateThru) as Datethru format=mmddyy10.
from (select * from temp group by id having f=max(f))
group by id, f ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 26 Dec 2018 21:47:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Collapsing-table-based-on-conditions/m-p/523588#M142256</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-12-26T21:47:25Z</dc:date>
    </item>
    <item>
      <title>Re: Collapsing table based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Collapsing-table-based-on-conditions/m-p/523590#M142257</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/183090"&gt;@rlafond&lt;/a&gt; - and here is yet a third way that doesn't require any joining of tables:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id datefrom :mmddyy10. datethru :mmddyy10.;
  format date: date9.;
datalines;
123 10/1/2004 5/25/2006
123 5/26/2006 8/28/2007
123 8/29/2007 3/17/2008
123 3/18/2008 10/14/2008
123 12/7/2015 12/31/2015
123 2/12/2016 12/31/9999
221 1/1/2005 5/26/2008 
221 11/11/2011 8/1/2015 
221 8/2/2015 10/12/2015 
221 10/13/2015 6/30/2018 
221 7/1/2018 12/31/9999
; 
run;

proc sort data =  have;
  by id datefrom;
run;

data want;
  keep id datefrom_new datethru;
  rename datefrom_new = datefrom; 
  set have;
  by id;
  retain datefrom_new datethru_last;
  format datefrom_new date9.;
  if first.id then do;
    datethru_last = .;
    datefrom_new = datefrom;
  end;
  else do;
    if datefrom - datethru_last &amp;gt; 1 then datefrom_new = datefrom;
  end;
  if last.id then output; 
  datethru_last = datethru;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 26 Dec 2018 23:42:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Collapsing-table-based-on-conditions/m-p/523590#M142257</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2018-12-26T23:42:49Z</dc:date>
    </item>
    <item>
      <title>Re: Collapsing table based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Collapsing-table-based-on-conditions/m-p/523595#M142261</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data result;
	set a;
        by id datefrom;
	retain nstart nend;
	if first.id or datefrom^=nend+1 then nstart=datefrom;
	nend=datethru;
	if last.id then do;
		datefrom=nstart;output;
	end;
	drop nstart nend;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;A title="refrence to" href="https://communities.sas.com/t5/New-SAS-User/How-do-I-reduce-duplicate-rows-observations-by-keeping-records/m-p/521856#M4304" target="_self"&gt;https://communities.sas.com/t5/New-SAS-User/How-do-I-reduce-duplicate-rows-observations-by-keeping-records/m-p/521856#M4304&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Dec 2018 01:52:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Collapsing-table-based-on-conditions/m-p/523595#M142261</guid>
      <dc:creator>learsaas</dc:creator>
      <dc:date>2018-12-27T01:52:34Z</dc:date>
    </item>
    <item>
      <title>Re: Collapsing table based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Collapsing-table-based-on-conditions/m-p/523636#M142276</link>
      <description>This wasn't the first answer, but I'm a sucker for DO loops. Thank you!</description>
      <pubDate>Thu, 27 Dec 2018 14:36:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Collapsing-table-based-on-conditions/m-p/523636#M142276</guid>
      <dc:creator>rlafond</dc:creator>
      <dc:date>2018-12-27T14:36:49Z</dc:date>
    </item>
  </channel>
</rss>

