<?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: Compare date in nth row with date in nth-3 row and Output in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Compare-date-in-nth-row-with-date-in-nth-3-row-and-Output/m-p/622250#M183038</link>
    <description>Thank you for the response. Worked like a charm.</description>
    <pubDate>Tue, 04 Feb 2020 17:42:33 GMT</pubDate>
    <dc:creator>shasank</dc:creator>
    <dc:date>2020-02-04T17:42:33Z</dc:date>
    <item>
      <title>Compare date in nth row with date in nth-3 row and Output</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-date-in-nth-row-with-date-in-nth-3-row-and-Output/m-p/622050#M182943</link>
      <description>&lt;P&gt;Hi SAS community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would appreciate your help in below problem:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a scenario in which a data-set sorted by ID and startdate and Enddate.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Each ID has multiple rows with startdate and enddate. If any 3 consecutive rows(Bunch of 3) have a date difference of &amp;lt;=60 months (1800 days) then Output.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For Ex: If the nth row of ID #1213 has an enddate- 01/12/2018 and nth-3 row has startdate- 31/12/2019 then that row should be flagged. Here is a sample dataset and expected output.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Row 3 startdate was compared to row 1 enddate and difference is 478 days. &amp;lt;1800 days or 60 months&amp;nbsp;&lt;/P&gt;&lt;P&gt;Row 4 startdate was compared to row 2 enddate and difference is -30 days.&amp;nbsp;&amp;lt;1800 days or 60 months&amp;nbsp;&lt;/P&gt;&lt;P&gt;Row 5 startdate was compared to row 3 enddate and difference is 200 days.&amp;nbsp;&amp;lt;1800 days or 60 months&amp;nbsp;&lt;/P&gt;&lt;P&gt;Row 6 startdate was compared to row 4 enddate and difference is 1965 days. &amp;gt; 1800 days or 60 months&lt;/P&gt;&lt;P&gt;So this should not be in the output.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Basically every startdate should be matched with n-3 end date and flagged for output. I tried to use lag function but it seems to only look for n-1.&lt;/P&gt;&lt;P&gt;Any help is appreciated. Thank you for your time and effort.&amp;nbsp;&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;Startdate&lt;/TD&gt;&lt;TD&gt;Enddate&lt;/TD&gt;&lt;TD&gt;Diff N and N -3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1122&lt;/TD&gt;&lt;TD&gt;1/12/2017&lt;/TD&gt;&lt;TD&gt;1/30/2017&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1122&lt;/TD&gt;&lt;TD&gt;4/3/2018&lt;/TD&gt;&lt;TD&gt;7/22/2018&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1122&lt;/TD&gt;&lt;TD&gt;5/23/2018&lt;/TD&gt;&lt;TD&gt;6/2/2018&lt;/TD&gt;&lt;TD&gt;478&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1122&lt;/TD&gt;&lt;TD&gt;6/22/2018&lt;/TD&gt;&lt;TD&gt;7/7/2018&lt;/TD&gt;&lt;TD&gt;-30&lt;/TD&gt;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&lt;TD&gt;1122&lt;/TD&gt;&lt;TD&gt;43453&lt;/TD&gt;&lt;TD&gt;43465&lt;/TD&gt;&lt;TD&gt;200&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Mon, 03 Feb 2020 21:13:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-date-in-nth-row-with-date-in-nth-3-row-and-Output/m-p/622050#M182943</guid>
      <dc:creator>shasank</dc:creator>
      <dc:date>2020-02-03T21:13:10Z</dc:date>
    </item>
    <item>
      <title>Re: Compare date in nth row with date in nth-3 row and Output</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-date-in-nth-row-with-date-in-nth-3-row-and-Output/m-p/622055#M182948</link>
      <description>&lt;P&gt;Try lag3.&lt;/P&gt;</description>
      <pubDate>Mon, 03 Feb 2020 21:31:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-date-in-nth-row-with-date-in-nth-3-row-and-Output/m-p/622055#M182948</guid>
      <dc:creator>JeffMaggio</dc:creator>
      <dc:date>2020-02-03T21:31:03Z</dc:date>
    </item>
    <item>
      <title>Re: Compare date in nth row with date in nth-3 row and Output</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-date-in-nth-row-with-date-in-nth-3-row-and-Output/m-p/622063#M182952</link>
      <description>&lt;P&gt;Actually, based on your sample and desired output, you want n-2, which is lag2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID	cStartdate $	cEnddate $	Diff;
cards;
1122 01/12/17 01/30/17 0	 
1122 04/03/18 07/22/18  0
1122 05/23/18 06/02/18 478
1122 06/22/18 07/07/18 -30
1122 12/19/18 12/31/18 200
1122 11/23/23 12/09/23 1965
1122 03/22/24 04/13/24 1908
;
run;

data want (drop=cStartdate cEnddate dif2);
set have;
startdate=input(cstartdate,mmddyy12.);
enddate=input(cenddate,mmddyy12.);
dif2 = lag2(startdate)-enddate;
format startdate date9. enddate date9.;
if abs(dif2) &amp;lt;= 1800 and dif2 ne .;

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 03 Feb 2020 22:00:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-date-in-nth-row-with-date-in-nth-3-row-and-Output/m-p/622063#M182952</guid>
      <dc:creator>JeffMaggio</dc:creator>
      <dc:date>2020-02-03T22:00:45Z</dc:date>
    </item>
    <item>
      <title>Re: Compare date in nth row with date in nth-3 row and Output</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-date-in-nth-row-with-date-in-nth-3-row-and-Output/m-p/622223#M183027</link>
      <description>&lt;P&gt;Thank you for your reply Jeff. I tried the code and it works wonderfully.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But, I wanted this calculation to go By ID and it seems to looks like this continues to include all the dates. I tried adding By ID and doesn't work. Is there any way to do this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sorry if I din't state this in my initial question.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your time.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Feb 2020 15:59:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-date-in-nth-row-with-date-in-nth-3-row-and-Output/m-p/622223#M183027</guid>
      <dc:creator>shasank</dc:creator>
      <dc:date>2020-02-04T15:59:22Z</dc:date>
    </item>
    <item>
      <title>Re: Compare date in nth row with date in nth-3 row and Output</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-date-in-nth-row-with-date-in-nth-3-row-and-Output/m-p/622227#M183029</link>
      <description>&lt;P&gt;To handle by group processing you need to add BY statement.&amp;nbsp; You also need to add logic to prevent it from checking dates from previous group.&amp;nbsp; That part is a little harder when lagging by more than one since you can't just test FIRST.ID to know when to ignore the lagged value.&amp;nbsp; You could use LAG() again to help.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=cStartdate cEnddate dif2);
  set have;
  by id;
  startdate=input(cstartdate,mmddyy12.);
  enddate=input(cenddate,mmddyy12.);
  format startdate date9. enddate date9.;

  dif2 = lag2(startdate)-enddate;
  if first.id or lag(first.id) then dif2=.;
  if abs(dif2) &amp;lt;= 1800 and dif2 ne .;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For the more general case create an observation counter.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Feb 2020 16:18:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-date-in-nth-row-with-date-in-nth-3-row-and-Output/m-p/622227#M183029</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-02-04T16:18:48Z</dc:date>
    </item>
    <item>
      <title>Re: Compare date in nth row with date in nth-3 row and Output</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-date-in-nth-row-with-date-in-nth-3-row-and-Output/m-p/622250#M183038</link>
      <description>Thank you for the response. Worked like a charm.</description>
      <pubDate>Tue, 04 Feb 2020 17:42:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-date-in-nth-row-with-date-in-nth-3-row-and-Output/m-p/622250#M183038</guid>
      <dc:creator>shasank</dc:creator>
      <dc:date>2020-02-04T17:42:33Z</dc:date>
    </item>
  </channel>
</rss>

