<?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: Date sequence Problem in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Date-sequence-Problem/m-p/737590#M229974</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;: Sorry by mistake i clicked on solution button.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;here is my input and output requirement.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input SUBJECT START_DATE $9. END_DATE $10. seq;
cards;
3008 01OCT2020 01OCT2020 1
3008 02OCT2020 04OCT2020 2
3008 05OCT2020 06OCT2020 3
3008 08OCT2020 10OCT2020 4
3009 03OCT2020 03OCT2020 1
3009 04OCT2020 05OCT2020 2
3009 05OCT2020 05OCT2020 3 
3009 06OCT2020 07OCT2020 4
3010 13OCT2020 15OCT2020 1
3010 17OCT2020 18OCT2020 2
;
run;


Output:

First Dataset:

SUBJECT START_DATE END_DATE
3008    01OCT2020  06OCT2020 
3009	03OCT2020  07OCT2020
3010	13OCT2020  15OCT2020 

Second Dataset:

SUBJECT START_DATE END_DATE
3008	 08OCT2020 10OCT2020 
3010	 17OCT2020 18OCT2020 

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Thank you&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 28 Apr 2021 14:32:05 GMT</pubDate>
    <dc:creator>singhsahab</dc:creator>
    <dc:date>2021-04-28T14:32:05Z</dc:date>
    <item>
      <title>Date sequence Problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-sequence-Problem/m-p/737582#M229971</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;request you to please help me to solve below problem.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input SUBJECT START_DATE $9. END_DATE $10. seq;
cards;
3008 01OCT2020 01OCT2020 1
3008 02OCT2020 04OCT2020 2
3008 05OCT2020 06OCT2020 3
3008 08OCT2020 05OCT2020 4
3009 03OCT2020 03OCT2020 1
3009 04OCT2020 05OCT2020 2
3009 05OCT2020 05OCT2020 3 
3009 06OCT2020 07OCT2020 4
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;May requirement is:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. If First event has started and ended, after ending first event the second event must be start next to first ending date then min START_DATE and max END_DATE need to be consider in final output as First dataset.&lt;/P&gt;
&lt;P&gt;2. If first event end date and second event start date has more then one day difference then it must be flagged. in second dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Apr 2021 14:13:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-sequence-Problem/m-p/737582#M229971</guid>
      <dc:creator>singhsahab</dc:creator>
      <dc:date>2021-04-28T14:13:43Z</dc:date>
    </item>
    <item>
      <title>Re: Date sequence Problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-sequence-Problem/m-p/737586#M229972</link>
      <description>Please show what you expect as output, if this is the given input.</description>
      <pubDate>Wed, 28 Apr 2021 14:21:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-sequence-Problem/m-p/737586#M229972</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-04-28T14:21:09Z</dc:date>
    </item>
    <item>
      <title>Re: Date sequence Problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-sequence-Problem/m-p/737590#M229974</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;: Sorry by mistake i clicked on solution button.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;here is my input and output requirement.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input SUBJECT START_DATE $9. END_DATE $10. seq;
cards;
3008 01OCT2020 01OCT2020 1
3008 02OCT2020 04OCT2020 2
3008 05OCT2020 06OCT2020 3
3008 08OCT2020 10OCT2020 4
3009 03OCT2020 03OCT2020 1
3009 04OCT2020 05OCT2020 2
3009 05OCT2020 05OCT2020 3 
3009 06OCT2020 07OCT2020 4
3010 13OCT2020 15OCT2020 1
3010 17OCT2020 18OCT2020 2
;
run;


Output:

First Dataset:

SUBJECT START_DATE END_DATE
3008    01OCT2020  06OCT2020 
3009	03OCT2020  07OCT2020
3010	13OCT2020  15OCT2020 

Second Dataset:

SUBJECT START_DATE END_DATE
3008	 08OCT2020 10OCT2020 
3010	 17OCT2020 18OCT2020 

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Thank you&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Apr 2021 14:32:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-sequence-Problem/m-p/737590#M229974</guid>
      <dc:creator>singhsahab</dc:creator>
      <dc:date>2021-04-28T14:32:05Z</dc:date>
    </item>
    <item>
      <title>Re: Date sequence Problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-sequence-Problem/m-p/737600#M229980</link>
      <description>&lt;P&gt;Sounds like you just want to collapse overlapping (or close to overlapping) periods into one period.&amp;nbsp; Not sure why you want two datasets out when one dataset can contain all of the periods with just a separate variable to indicate which period it is.&lt;/P&gt;
&lt;P&gt;So first make sure your dates are actual date variables (and not strings like you example).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input SUBJECT (START_DATE END_DATE) (:date.) seq;
  format start_date end_date date9.;
cards;
3008 01OCT2020 01OCT2020 1
3008 02OCT2020 04OCT2020 2
3008 05OCT2020 06OCT2020 3
3008 08OCT2020 10OCT2020 4
3009 03OCT2020 03OCT2020 1
3009 04OCT2020 05OCT2020 2
3009 05OCT2020 05OCT2020 3
3009 06OCT2020 07OCT2020 4
3010 13OCT2020 15OCT2020 1
3010 17OCT2020 18OCT2020 2
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then generate a PERIOD variable to flag the overlapping periods.&amp;nbsp; Adjust the IF condition if you want to allow different size gap between start and end to be considered part of the same large period.&lt;/P&gt;
&lt;P&gt;Including START_DATE and END_DATE in the BY statement will cause the data step to insure the observations are sorted properly.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data periods ;
  set have ;
  by subject start_date end_date ;
  lag_end = lag(end_date);
  if first.subject then period=1;
  else if start_date &amp;gt; lag_end+1 then period+1;
  drop lag_end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now collapse to one observation per derived period.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set periods ;
  by subject period;
  if first.period then start=start_date ;
  if last.period then do;
    start_date=start;
    output;
  end;
  retain start;
  drop start seq;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;                     START_
Obs    SUBJECT         DATE     END_DATE    period

 1       3008     01OCT2020    06OCT2020       1
 2       3008     08OCT2020    10OCT2020       2
 3       3009     03OCT2020    07OCT2020       1
 4       3010     13OCT2020    15OCT2020       1
 5       3010     17OCT2020    18OCT2020       2
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note if the original start/end date data is messier with overlapping or nested time periods you will need more complex logic to determine the overlapping periods.&lt;/P&gt;</description>
      <pubDate>Wed, 28 Apr 2021 16:29:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-sequence-Problem/m-p/737600#M229980</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-04-28T16:29:57Z</dc:date>
    </item>
  </channel>
</rss>

