<?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 to collapse time intervals contingent upon some arguments in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-time-intervals-contingent-upon-some-arguments/m-p/895949#M353988</link>
    <description>&lt;P&gt;&lt;SPAN&gt;I would like to combine two or more consecutive time periods into one, based on the condition that they have the same ID and the same value for another variable (hours). A new record in the output data will be generated if there is a new ID or a change in the value of hours within an ID. Additionally, if there is a break or a gap between the Enddate of one period and the Startdate of the next period within an ID, a new record will also be created in the output data.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Data Have:&lt;/P&gt;
&lt;PRE&gt;Rec ID Startdate Enddate Hours&lt;BR /&gt;1 1 01/01/2023 01/08/2023 8&lt;BR /&gt;2 1 01/09/2023 01/31/2023 8&lt;BR /&gt;3 1 02/01/2023 02/28/2023 4&lt;BR /&gt;4 1 03/01/2023 05/31/2023 8&lt;BR /&gt;5 1 06/01/2023 09/26/2023 8&lt;BR /&gt;6 2 01/01/2023 01/07/2023 8&lt;BR /&gt;7 2 01/10/2023 01/31/2023 8&lt;BR /&gt;8 2 02/01/2023 02/28/2023 4&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Data Want:&lt;/P&gt;
&lt;PRE&gt;ID Startdate Enddate Hours&lt;BR /&gt;1 01/01/2023 31/01/2023 8&lt;BR /&gt;1 02/01/2023 02/28/2023 4&lt;BR /&gt;1 03/01/2023 09/26/2023 8&lt;BR /&gt;2 01/01/2023 01/07/2023 8&lt;BR /&gt;2 01/10/2023 01/31/2023 8&lt;BR /&gt;2 02/01/2023 02/28/2023 4&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Based on a former discussion in SAS Communities I’m working on this code which doesn’t fully make it (for example, it doesn’t collapse record 4 and 5):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;proc sort data=have out=have2;
  by id hours startdate;
run;

data want;
  set have2;
  by id hours;

  lag_enddate=lag(enddate);
  if not first.hours then do;
    if startdate-lag_enddate&amp;lt;=2 then delete_flag=1;
  end;
  if delete_flag then delete;
  drop lag_enddate delete_flag;
run;
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 26 Sep 2023 19:40:03 GMT</pubDate>
    <dc:creator>terjeph</dc:creator>
    <dc:date>2023-09-26T19:40:03Z</dc:date>
    <item>
      <title>How to collapse time intervals contingent upon some arguments</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-time-intervals-contingent-upon-some-arguments/m-p/895949#M353988</link>
      <description>&lt;P&gt;&lt;SPAN&gt;I would like to combine two or more consecutive time periods into one, based on the condition that they have the same ID and the same value for another variable (hours). A new record in the output data will be generated if there is a new ID or a change in the value of hours within an ID. Additionally, if there is a break or a gap between the Enddate of one period and the Startdate of the next period within an ID, a new record will also be created in the output data.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Data Have:&lt;/P&gt;
&lt;PRE&gt;Rec ID Startdate Enddate Hours&lt;BR /&gt;1 1 01/01/2023 01/08/2023 8&lt;BR /&gt;2 1 01/09/2023 01/31/2023 8&lt;BR /&gt;3 1 02/01/2023 02/28/2023 4&lt;BR /&gt;4 1 03/01/2023 05/31/2023 8&lt;BR /&gt;5 1 06/01/2023 09/26/2023 8&lt;BR /&gt;6 2 01/01/2023 01/07/2023 8&lt;BR /&gt;7 2 01/10/2023 01/31/2023 8&lt;BR /&gt;8 2 02/01/2023 02/28/2023 4&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Data Want:&lt;/P&gt;
&lt;PRE&gt;ID Startdate Enddate Hours&lt;BR /&gt;1 01/01/2023 31/01/2023 8&lt;BR /&gt;1 02/01/2023 02/28/2023 4&lt;BR /&gt;1 03/01/2023 09/26/2023 8&lt;BR /&gt;2 01/01/2023 01/07/2023 8&lt;BR /&gt;2 01/10/2023 01/31/2023 8&lt;BR /&gt;2 02/01/2023 02/28/2023 4&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Based on a former discussion in SAS Communities I’m working on this code which doesn’t fully make it (for example, it doesn’t collapse record 4 and 5):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;proc sort data=have out=have2;
  by id hours startdate;
run;

data want;
  set have2;
  by id hours;

  lag_enddate=lag(enddate);
  if not first.hours then do;
    if startdate-lag_enddate&amp;lt;=2 then delete_flag=1;
  end;
  if delete_flag then delete;
  drop lag_enddate delete_flag;
run;
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Sep 2023 19:40:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-time-intervals-contingent-upon-some-arguments/m-p/895949#M353988</guid>
      <dc:creator>terjeph</dc:creator>
      <dc:date>2023-09-26T19:40:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to collapse time intervals contingent upon some arguments</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-time-intervals-contingent-upon-some-arguments/m-p/895950#M353989</link>
      <description>&lt;P&gt;Are you &lt;STRONG&gt;really&lt;/STRONG&gt; sure that these are supposed to "collapse"? The 5/31/2023 date is not included. Your rule actually states that if there is a gap they should not be collapsed but you ask specifically about records 4 and 5 that should.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;4 1 03/01/2023 05/30/2023 8&lt;BR /&gt;5 1 06/01/2023 09/26/2023 8&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Sep 2023 19:36:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-time-intervals-contingent-upon-some-arguments/m-p/895950#M353989</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-09-26T19:36:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to collapse time intervals contingent upon some arguments</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-time-intervals-contingent-upon-some-arguments/m-p/895951#M353990</link>
      <description>It should be 05/31/2023 - sorry</description>
      <pubDate>Tue, 26 Sep 2023 19:39:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-time-intervals-contingent-upon-some-arguments/m-p/895951#M353990</guid>
      <dc:creator>terjeph</dc:creator>
      <dc:date>2023-09-26T19:39:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to collapse time intervals contingent upon some arguments</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-time-intervals-contingent-upon-some-arguments/m-p/895976#M353999</link>
      <description>&lt;P&gt;Assuming the data are sorted by id/startdate, then&amp;nbsp; (untested in the absence of sample data in the form of a working DATA step):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_: nxt_:);
  set have;
  by id hours notsorted;
  merge have have (firstobs=2 keep=startdate rename=(startdate=nxt_startdate));

  retain _startdate;
  if first.hours or lag(enddate)&amp;lt;startdate-1 then _startdate=startdate;

  if last.hours or nxt_startdate &amp;gt; enddate+1;
  startdate=_startdate;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 26 Sep 2023 23:01:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-time-intervals-contingent-upon-some-arguments/m-p/895976#M353999</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-09-26T23:01:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to collapse time intervals contingent upon some arguments</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-time-intervals-contingent-upon-some-arguments/m-p/896021#M354011</link>
      <description>&lt;P&gt;Worked well! Thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 27 Sep 2023 11:05:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-time-intervals-contingent-upon-some-arguments/m-p/896021#M354011</guid>
      <dc:creator>terjeph</dc:creator>
      <dc:date>2023-09-27T11:05:20Z</dc:date>
    </item>
  </channel>
</rss>

