<?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 Dates across rows of observations with a twist. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Collapsing-Dates-across-rows-of-observations-with-a-twist/m-p/413023#M101049</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Left to my own devices, my approach would have been to restructure the data to look like&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data AlternateInput;
  input id mydate:mmddyy10. datetype;
  datalines;
1 1/1/2010 start
1 4/1/2010 end
1 4/24/2010 start
1 6/1/2010 end
1 9/1/2010 start
1 10/15/2010 end 
2 3/1/2010 start
2 5/1/2010 end
2 6/1/2010 start
2 10/1/2010 end
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and then try to figure out how to walk the ID's to find the periods.&amp;nbsp; Probably best I'm not left to solve it myself. Ha.&lt;/P&gt;</description>
    <pubDate>Mon, 13 Nov 2017 19:45:59 GMT</pubDate>
    <dc:creator>HB</dc:creator>
    <dc:date>2017-11-13T19:45:59Z</dc:date>
    <item>
      <title>Collapsing Dates across rows of observations with a twist.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Collapsing-Dates-across-rows-of-observations-with-a-twist/m-p/412951#M101013</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have an input data that looks like this. The data has two subjects with ID1 and ID2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp;Start_date&amp;nbsp; &amp;nbsp; &amp;nbsp; End_date&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;STRONG&gt;1/1/2010&lt;/STRONG&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4/1/2010&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4/24/2010&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;STRONG&gt;6/1/2010&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;STRONG&gt;9/1/2010&amp;nbsp;&lt;/STRONG&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;STRONG&gt;10/15/2010&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;STRONG&gt;3/1/2010&amp;nbsp;&lt;/STRONG&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;5/1/2010&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;6/1/2010&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;STRONG&gt;10/1/2010&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Basically, I want to collapse the multiple start and end dates for each subject into one contiguous start and end date per each subject. &lt;STRONG&gt;However, there is a twist.&amp;nbsp; If the gap between the end date and next start date for the subject is greater than 45 days, then the dates are not collapsed for that row of observation.&amp;nbsp; I have highlighted the desired start and end dates for each subject.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The output dataset should look like&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp;Start_date&amp;nbsp; &amp;nbsp; &amp;nbsp; End_date&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;STRONG&gt;1/1/2010&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;6/1/2010&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;STRONG&gt;9/1/2010&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;10/15/2010&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;STRONG&gt;3/1/2010&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;10/1/2010&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Note that subject 1 has a gap greater than 45 days (&lt;STRONG&gt;from 6/1/2010 to 9/1/2010),&lt;/STRONG&gt; so the row from&amp;nbsp;&lt;SPAN&gt;9/1/2010&amp;nbsp; to 10/15/2010 is not collapsed with the first two rows for subject 1.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Subject 2 has no gap greater than 45 days, so his two rows of start and end dates are collapsed.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much for your help.&lt;/P&gt;&lt;P&gt;gary&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Nov 2017 16:18:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Collapsing-Dates-across-rows-of-observations-with-a-twist/m-p/412951#M101013</guid>
      <dc:creator>pegasus1225</dc:creator>
      <dc:date>2017-11-13T16:18:17Z</dc:date>
    </item>
    <item>
      <title>Re: Collapsing Dates across rows of observations with a twist.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Collapsing-Dates-across-rows-of-observations-with-a-twist/m-p/412955#M101015</link>
      <description>&lt;P&gt;&lt;STRONG&gt;Post test data in the form of a datastep!&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;As such I am not typing that in to test this, and I assume that is a date.&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  retain start_date_ end_date_;
  by id;
  if first.id or start_date_=. then start_date_=start_date;
  else do;
    if last.id or end_date-lag(end_date) &amp;gt; 45 then do;
      end_date_=end_date;
      output;
      start_date_=.;
      end_date_=.;
    end;
   end;
run;
      &lt;/PRE&gt;
&lt;P&gt;Note the output variables - the want, is with a _ after.&lt;/P&gt;</description>
      <pubDate>Mon, 13 Nov 2017 16:34:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Collapsing-Dates-across-rows-of-observations-with-a-twist/m-p/412955#M101015</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-11-13T16:34:04Z</dc:date>
    </item>
    <item>
      <title>Re: Collapsing Dates across rows of observations with a twist.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Collapsing-Dates-across-rows-of-observations-with-a-twist/m-p/412960#M101019</link>
      <description>&lt;P&gt;I think it would be easiest for you to follow the logic if you were to break this into two steps (even though it can be done in one).&amp;nbsp; Here is an example of the first step.&amp;nbsp; Assuming your data is already sorted by ID START_DATE:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have2;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;by id;&lt;/P&gt;
&lt;P&gt;prior_end = lag(end_date);&lt;/P&gt;
&lt;P&gt;if first.id or (start_date - prior_end) &amp;gt; 45 then grouping + 1;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then you can take a look at the result, and determine whether GROUPING matches your expectations.&amp;nbsp; If it does, continue with the second step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have2;&lt;/P&gt;
&lt;P&gt;by id grouping;&lt;/P&gt;
&lt;P&gt;if first.grouping then new_start = start_date;&lt;/P&gt;
&lt;P&gt;retain new_start;&lt;/P&gt;
&lt;P&gt;drop start_date;&lt;/P&gt;
&lt;P&gt;rename new_start = start_date;&lt;/P&gt;
&lt;P&gt;if last.grouping;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Nov 2017 16:57:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Collapsing-Dates-across-rows-of-observations-with-a-twist/m-p/412960#M101019</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-11-13T16:57:13Z</dc:date>
    </item>
    <item>
      <title>Re: Collapsing Dates across rows of observations with a twist.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Collapsing-Dates-across-rows-of-observations-with-a-twist/m-p/412965#M101021</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt; I'm not sure that quite works.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data Input;
  input id start_date:mmddyy10. end_date:mmddyy10.;
  datalines;
1 1/1/2010 4/1/2010
1 4/24/2010 6/1/2010
1 9/1/2010 10/15/2010 
2 3/1/2010 5/1/2010
2 6/1/2010 10/1/2010
;
run;

data output;
  set input;
  retain start_date_ end_date_;
  by id;
  if first.id or start_date_=. then start_date_=start_date;
  else do;
    if last.id or end_date-lag(end_date) &amp;gt; 45 then do;
      end_date_=end_date;
      output;
      start_date_=.;
      end_date_=.;
    end;
   end;
run;

proc print data = output noobs;
	var id start_date_ end_date_;
	format start_date_ date9. end_date_ date9.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I get this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;                                       The SAS System      

                                           start_
                                  id        date_    end_date_

                                   1    01JAN2010    15OCT2010
                                   2    01MAR2010    01OCT2010

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which I don't think is exactly what OP wants.&amp;nbsp; It seems to skip over the 6/1 to 9/1 break for ID 1 in which OP is interested.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Nov 2017 17:19:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Collapsing-Dates-across-rows-of-observations-with-a-twist/m-p/412965#M101021</guid>
      <dc:creator>HB</dc:creator>
      <dc:date>2017-11-13T17:19:13Z</dc:date>
    </item>
    <item>
      <title>Re: Collapsing Dates across rows of observations with a twist.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Collapsing-Dates-across-rows-of-observations-with-a-twist/m-p/412966#M101022</link>
      <description>&lt;P&gt;Using the data from above:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have2;
	set input;
	by id;
	prior_end = lag(end_date);
	if first.id or (start_date - prior_end) &amp;gt; 45 then grouping + 1;
run;

data want;
	set have2;
	by id grouping;
	if first.grouping then new_start = start_date;
	retain new_start;
	drop start_date;
	rename new_start = start_date;
	if last.grouping;
run;

proc print data = want noobs;
	var id start_date end_date;
	format start_date date9. end_date date9.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Seems to yield&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;                                         The SAS System 

                                           start_
                                  id         date     end_date

                                   1    01JAN2010    01JUN2010
                                   1    01SEP2010    15OCT2010
                                   2    01MAR2010    01OCT2010

&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;I wish I understood either of the above approaches.&amp;nbsp; Lol.&amp;nbsp; &lt;/P&gt;</description>
      <pubDate>Mon, 13 Nov 2017 17:29:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Collapsing-Dates-across-rows-of-observations-with-a-twist/m-p/412966#M101022</guid>
      <dc:creator>HB</dc:creator>
      <dc:date>2017-11-13T17:29:06Z</dc:date>
    </item>
    <item>
      <title>Re: Collapsing Dates across rows of observations with a twist.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Collapsing-Dates-across-rows-of-observations-with-a-twist/m-p/412993#M101033</link>
      <description>&lt;P&gt;Here is a single DATA step processs that takes advantage of executing lag functions selectively, as well as a self-merge with FIRSTOBS=2:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
 input ID     (Start_date      End_date) (:mmddyy10.);
 format start_date end_date date9.;
datalines;
1       1/1/2010         4/1/2010
1       4/24/2010      6/1/2010
1      9/1/2010       10/15/2010 
2       3/1/2010       5/1/2010
2       6/1/2010      10/1/2010
run;


data want (drop=next_:);

  merge have
        have (firstobs=2 keep=id start_date rename=(id=next_id start_date=next_start));

  if next_id^=id or next_start&amp;gt;end_date+45;

  start_date=lag(next_start);
  if start_date=. then set have (keep=start_date obs=1);
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;Notes:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The merge statement matches record&amp;nbsp;N of HAVE with record N+1 (yielding new vars NEXT_ID and NEXT_START.&lt;/LI&gt;
&lt;LI&gt;The subsetting IF statement only allow records from the end of a qualified period - therefore providing the desired END_DATE&lt;/LI&gt;
&lt;LI&gt;The start_date=lag(next_date) only update the lag(next_date) queue when a complete period has been identified.&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;The lag(next_date) value is missing for the first desired period, so observation 1 is re-read only for that instance.&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Mon, 13 Nov 2017 18:09:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Collapsing-Dates-across-rows-of-observations-with-a-twist/m-p/412993#M101033</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-11-13T18:09:16Z</dc:date>
    </item>
    <item>
      <title>Re: Collapsing Dates across rows of observations with a twist.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Collapsing-Dates-across-rows-of-observations-with-a-twist/m-p/413006#M101040</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/6401"&gt;@HB&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To understand how this solution works, inspect the intermediate data set more carefully.&amp;nbsp; See if&amp;nbsp; you understand how GROUPING is being created.&amp;nbsp; (Note that it might in theory be the same for 3 or more observations, not just for two observations.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Once you understand what is in GROUPING, think about how you could use GROUPING to create the final data set.&amp;nbsp; Then compare your thoughts to the second DATA part of the solution.&lt;/P&gt;</description>
      <pubDate>Mon, 13 Nov 2017 18:28:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Collapsing-Dates-across-rows-of-observations-with-a-twist/m-p/413006#M101040</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-11-13T18:28:34Z</dc:date>
    </item>
    <item>
      <title>Re: Collapsing Dates across rows of observations with a twist.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Collapsing-Dates-across-rows-of-observations-with-a-twist/m-p/413023#M101049</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Left to my own devices, my approach would have been to restructure the data to look like&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data AlternateInput;
  input id mydate:mmddyy10. datetype;
  datalines;
1 1/1/2010 start
1 4/1/2010 end
1 4/24/2010 start
1 6/1/2010 end
1 9/1/2010 start
1 10/15/2010 end 
2 3/1/2010 start
2 5/1/2010 end
2 6/1/2010 start
2 10/1/2010 end
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and then try to figure out how to walk the ID's to find the periods.&amp;nbsp; Probably best I'm not left to solve it myself. Ha.&lt;/P&gt;</description>
      <pubDate>Mon, 13 Nov 2017 19:45:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Collapsing-Dates-across-rows-of-observations-with-a-twist/m-p/413023#M101049</guid>
      <dc:creator>HB</dc:creator>
      <dc:date>2017-11-13T19:45:59Z</dc:date>
    </item>
    <item>
      <title>Re: Collapsing Dates across rows of observations with a twist.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Collapsing-Dates-across-rows-of-observations-with-a-twist/m-p/413063#M101061</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;SPAN class="login-bold"&gt;RW9, HB, Astounding and Mkeintz&amp;nbsp;for your big help.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="login-bold"&gt;lag function is very useful.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Nov 2017 21:09:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Collapsing-Dates-across-rows-of-observations-with-a-twist/m-p/413063#M101061</guid>
      <dc:creator>pegasus1225</dc:creator>
      <dc:date>2017-11-13T21:09:10Z</dc:date>
    </item>
  </channel>
</rss>

