<?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: Looking back to get eligible records in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Looking-back-to-get-eligible-records/m-p/790314#M253010</link>
    <description>&lt;P&gt;So your START_DATE and PREVIOUS_DATE variables really represent START and END dates (AKA an interval)?&amp;nbsp; And your goal is to collapse the overlapping/contiguous intervals into a single interval?&lt;/P&gt;</description>
    <pubDate>Sat, 15 Jan 2022 02:49:07 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-01-15T02:49:07Z</dc:date>
    <item>
      <title>Looking back to get eligible records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looking-back-to-get-eligible-records/m-p/790272#M252990</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;SPAN&gt;I have attached some example input and output datasets. &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;So here the&amp;nbsp;&lt;/SPAN&gt;Start_date for ids's done after the first one must be later than the previous_date. If Start_date for the current record is before or equal to the previous previous_date,&amp;nbsp; I need to remove the record. Then I need to create a sequence number for each record within ID, where&amp;nbsp;&lt;SPAN&gt;Counter resets for each new ID.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;After the first record I need to look to the previous date from second record onwards and create a sequence.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Taking EFG ID as an example : the record with start_date 10DEC2021 wont be an eligible record because the previous_date&lt;BR /&gt;for previous record is 12dec2021 so the current records start date(10dec2021) is before the previous records previous date thus this record is excluded.&lt;BR /&gt;And for 13DEC2021 startdate record it will be eligible record whereas 16DEC2021 wont be eligible record as the date of previous eligible record previous_date(16DEC2021) is equal to this current records start_date.&lt;BR /&gt;I tried using lag and counting the rows but with lag its an lengthy procedure to count all previous rows.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Input dataset:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; Start_date&amp;nbsp; &amp;nbsp; Previous_date&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;ABC 03AUG2020 08AUG2020&lt;BR /&gt;ABC 11AUG2020 12AUG2020&lt;BR /&gt;ABC 03NOV2020 05NOV2020&lt;BR /&gt;ABC 09NOV2020 16FEB2021&lt;BR /&gt;ABC 20NOV2020 24NOV2020&lt;BR /&gt;ABC 07DEC2020 13DEC2020&lt;BR /&gt;ABC 17DEC2020 24DEC2020&lt;BR /&gt;ABC 25JAN2021 29JAN2021&lt;BR /&gt;ABC 24FEB2021 15MAR2021&lt;BR /&gt;ABC 18MAR2021 22MAR2021&lt;BR /&gt;ABC 12APR2021 15APR2021&lt;BR /&gt;ABC 14APR2021 16APR2021&lt;BR /&gt;ABC 20APR2021 28APR2021&lt;BR /&gt;ABC 19MAY2021 26MAY2021&lt;BR /&gt;EFG 08MAR2021 10MAR2021&lt;BR /&gt;EFG 08APR2021 15APR2021&lt;BR /&gt;EFG 30APR2021 06MAY2021&lt;BR /&gt;EFG 18MAY2021 25MAY2021&lt;BR /&gt;EFG 20AUG2021 23AUG2021&lt;BR /&gt;EFG 08SEP2021 10SEP2021&lt;BR /&gt;EFG 06OCT2021 07OCT2021&lt;BR /&gt;EFG 22NOV2021 23NOV2021&lt;BR /&gt;EFG 07DEC2021 12DEC2021&lt;BR /&gt;EFG 10DEC2021 13DEC2021&lt;BR /&gt;EFG 13DEC2021 16DEC2021&lt;BR /&gt;EFG 16DEC2021 21DEC2021&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The output should be&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp;Start_date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Previous_date&amp;nbsp; Seq&lt;/P&gt;&lt;P&gt;ABC 03AUG2020 08AUG2020 1&lt;BR /&gt;ABC 11AUG2020 12AUG2020 2&lt;BR /&gt;ABC 03NOV2020 05NOV2020 3&lt;BR /&gt;ABC 09NOV2020 16FEB2021 4&lt;BR /&gt;ABC 24FEB2021 15MAR2021 5&lt;BR /&gt;ABC 18MAR2021 22MAR2021 6&lt;BR /&gt;ABC 12APR2021 15APR2021 7&lt;BR /&gt;ABC 20APR2021 28APR2021 8&lt;BR /&gt;ABC 19MAY2021 26MAY2021 9&lt;BR /&gt;EFG 08MAR2021 10MAR2021 1&lt;BR /&gt;EFG 08APR2021 15APR2021 2&lt;BR /&gt;EFG 30APR2021 06MAY2021 3&lt;BR /&gt;EFG 18MAY2021 25MAY2021 4&lt;BR /&gt;EFG 20AUG2021 23AUG2021 5&lt;BR /&gt;EFG 08SEP2021 10SEP2021 6&lt;BR /&gt;EFG 06OCT2021 07OCT2021 7&lt;BR /&gt;EFG 22NOV2021 23NOV2021 8&lt;BR /&gt;EFG 07DEC2021 12DEC2021 9&lt;BR /&gt;EFG 13DEC2021 16DEC2021 10&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;Any help is greatly appreciated. Thanks In Advance.&lt;/P&gt;</description>
      <pubDate>Fri, 14 Jan 2022 22:03:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looking-back-to-get-eligible-records/m-p/790272#M252990</guid>
      <dc:creator>Aidaan_10</dc:creator>
      <dc:date>2022-01-14T22:03:32Z</dc:date>
    </item>
    <item>
      <title>Re: Looking back to get eligible records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looking-back-to-get-eligible-records/m-p/790274#M252991</link>
      <description>Please include what you've tried with Lags etc.</description>
      <pubDate>Fri, 14 Jan 2022 22:04:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looking-back-to-get-eligible-records/m-p/790274#M252991</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-01-14T22:04:35Z</dc:date>
    </item>
    <item>
      <title>Re: Looking back to get eligible records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looking-back-to-get-eligible-records/m-p/790276#M252992</link>
      <description>I tried to get the prior previous_Date from the valid eligible record.</description>
      <pubDate>Fri, 14 Jan 2022 22:11:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looking-back-to-get-eligible-records/m-p/790276#M252992</guid>
      <dc:creator>Aidaan_10</dc:creator>
      <dc:date>2022-01-14T22:11:15Z</dc:date>
    </item>
    <item>
      <title>Re: Looking back to get eligible records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looking-back-to-get-eligible-records/m-p/790296#M253000</link>
      <description>&lt;P&gt;Here is how to provide the example data as a date step so we can use that data.&lt;/P&gt;
&lt;PRE&gt;data have;
   input ID $ Start_date :Date9. Previous_date :Date9.;
   format start_date previous_date date9.;
datalines;
ABC 03AUG2020 08AUG2020
ABC 11AUG2020 12AUG2020
ABC 03NOV2020 05NOV2020
ABC 09NOV2020 16FEB2021
ABC 20NOV2020 24NOV2020
ABC 07DEC2020 13DEC2020
ABC 17DEC2020 24DEC2020
ABC 25JAN2021 29JAN2021
ABC 24FEB2021 15MAR2021
ABC 18MAR2021 22MAR2021
ABC 12APR2021 15APR2021
ABC 14APR2021 16APR2021
ABC 20APR2021 28APR2021
ABC 19MAY2021 26MAY2021
EFG 08MAR2021 10MAR2021
EFG 08APR2021 15APR2021
EFG 30APR2021 06MAY2021
EFG 18MAY2021 25MAY2021
EFG 20AUG2021 23AUG2021
EFG 08SEP2021 10SEP2021
EFG 06OCT2021 07OCT2021
EFG 22NOV2021 23NOV2021
EFG 07DEC2021 12DEC2021
EFG 10DEC2021 13DEC2021
EFG 13DEC2021 16DEC2021
EFG 16DEC2021 21DEC2021
;
&lt;/PRE&gt;
&lt;P&gt;Now you need to explain why in these records the 4 following the 09NOV2020 start_date are removed. Yes 20NOV2020 is before 16FEF2021 but 07DEC2020 is &lt;STRONG&gt;not&lt;/STRONG&gt; before 24NOV2020. So your actual output is apparently using a rule that is much more than looking at the record immediately before, which is much more complex than your description.&lt;/P&gt;
&lt;PRE&gt;ABC 09NOV2020 16FEB2021
ABC 20NOV2020 24NOV2020
ABC 07DEC2020 13DEC2020
ABC 17DEC2020 24DEC2020
ABC 25JAN2021 29JAN2021&lt;/PRE&gt;</description>
      <pubDate>Fri, 14 Jan 2022 22:49:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looking-back-to-get-eligible-records/m-p/790296#M253000</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-01-14T22:49:35Z</dc:date>
    </item>
    <item>
      <title>Re: Looking back to get eligible records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looking-back-to-get-eligible-records/m-p/790297#M253001</link>
      <description>Apologies for posting the data in that manner. And Yes, you are right the logic is too complex then mentioned above the 16FEB2021 previous date record is the eligible record and the records after that are ineligible and the next eligible record will be the 24FEB2021 so basically we are not looking for just previous record date but we are looking for the eligible records previous date for next current record.</description>
      <pubDate>Fri, 14 Jan 2022 23:01:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looking-back-to-get-eligible-records/m-p/790297#M253001</guid>
      <dc:creator>Aidaan_10</dc:creator>
      <dc:date>2022-01-14T23:01:33Z</dc:date>
    </item>
    <item>
      <title>Re: Looking back to get eligible records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looking-back-to-get-eligible-records/m-p/790312#M253009</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/174192"&gt;@Aidaan_10&lt;/a&gt;&amp;nbsp;If I understand the required logic right then below code should return what you're asking for.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by id start_date;
  retain _l_previous_date;
  format _l_previous_date date9.;

  if not first.id then
    do;
      if (start_date-_l_previous_date)&amp;lt;=0 then delete;
    end;
  _l_previous_date=previous_date;
  drop _l_previous_date;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1642210657187.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/67428iAC89495D76251D7A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1642210657187.png" alt="Patrick_0-1642210657187.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You haven't asked for it so just guessing: Wouldn't you also need to amend the Previous_Date value? Like for&amp;nbsp;&lt;SPAN&gt;24FEB2021 the&amp;nbsp;Previous_Date value would become the max value derived from the deleted rows (=29JAN2021).&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 15 Jan 2022 01:40:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looking-back-to-get-eligible-records/m-p/790312#M253009</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-01-15T01:40:55Z</dc:date>
    </item>
    <item>
      <title>Re: Looking back to get eligible records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looking-back-to-get-eligible-records/m-p/790314#M253010</link>
      <description>&lt;P&gt;So your START_DATE and PREVIOUS_DATE variables really represent START and END dates (AKA an interval)?&amp;nbsp; And your goal is to collapse the overlapping/contiguous intervals into a single interval?&lt;/P&gt;</description>
      <pubDate>Sat, 15 Jan 2022 02:49:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looking-back-to-get-eligible-records/m-p/790314#M253010</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-01-15T02:49:07Z</dc:date>
    </item>
    <item>
      <title>Re: Looking back to get eligible records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looking-back-to-get-eligible-records/m-p/790323#M253014</link>
      <description>Thanks a ton Patrick, this is what I wanted. I was trying to achieve this via so many lag statements and flagging variables and all but this is great. Thanks again.</description>
      <pubDate>Sat, 15 Jan 2022 05:46:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looking-back-to-get-eligible-records/m-p/790323#M253014</guid>
      <dc:creator>Aidaan_10</dc:creator>
      <dc:date>2022-01-15T05:46:28Z</dc:date>
    </item>
  </channel>
</rss>

