<?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: Matching with related dates in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Matching-with-related-dates/m-p/322476#M71340</link>
    <description>&lt;P&gt;Pet peeve: Calling datetime values "dates".&lt;/P&gt;
&lt;P&gt;So are your datetime variables SAS datetime values (have a format like datetime20.) or are they character?&lt;/P&gt;</description>
    <pubDate>Wed, 04 Jan 2017 20:21:13 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2017-01-04T20:21:13Z</dc:date>
    <item>
      <title>Matching with related dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Matching-with-related-dates/m-p/322462#M71334</link>
      <description>&lt;P&gt;All, I have the following two data sets:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;have1:&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;Date1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;67749831&lt;/TD&gt;&lt;TD&gt;28AUG2015:08:49:00.000000&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;68011120&lt;/TD&gt;&lt;TD&gt;10SEP2015:11:03:00.000000&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;have2:&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;Date2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;67749831&lt;/TD&gt;&lt;TD&gt;26AUG2015:08:49:00.000000&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;67749831&lt;/TD&gt;&lt;TD&gt;27AUG2015:08:49:00.000000&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;67749831&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;29AUG2015:08:49:00.000000&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want the following dataset:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Want:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;Date1&lt;/TD&gt;&lt;TD&gt;Date2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;67749831&lt;/TD&gt;&lt;TD&gt;28AUG2015:08:49:00.000000&lt;/TD&gt;&lt;TD&gt;27AUG2015:08:49:00.000000&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;68011120&lt;/TD&gt;&lt;TD&gt;10SEP2015:11:03:00.000000&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The idea is to merge the two files based on the exact same ID, but I also want the Date2 to be the latest date that is still before Date1. There might be many Instances of a Date2 that come before Date1 (I want the most recent one), one instance, or no instances.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does anyone have any ideas?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 04 Jan 2017 19:02:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Matching-with-related-dates/m-p/322462#M71334</guid>
      <dc:creator>SThomas</dc:creator>
      <dc:date>2017-01-04T19:02:01Z</dc:date>
    </item>
    <item>
      <title>Re: Matching with related dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Matching-with-related-dates/m-p/322476#M71340</link>
      <description>&lt;P&gt;Pet peeve: Calling datetime values "dates".&lt;/P&gt;
&lt;P&gt;So are your datetime variables SAS datetime values (have a format like datetime20.) or are they character?&lt;/P&gt;</description>
      <pubDate>Wed, 04 Jan 2017 20:21:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Matching-with-related-dates/m-p/322476#M71340</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-01-04T20:21:13Z</dc:date>
    </item>
    <item>
      <title>Re: Matching with related dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Matching-with-related-dates/m-p/322485#M71344</link>
      <description>&lt;P&gt;Assuming both data sets are in order by both ID and date:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have2 (rename=(date2=date1) in=in2) have1;&lt;/P&gt;
&lt;P&gt;by id date1;&lt;/P&gt;
&lt;P&gt;if first.id then date2=.;&lt;/P&gt;
&lt;P&gt;retain date2;&lt;/P&gt;
&lt;P&gt;if in2 then date2=date1;&lt;/P&gt;
&lt;P&gt;else output;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Jan 2017 20:34:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Matching-with-related-dates/m-p/322485#M71344</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-01-04T20:34:43Z</dc:date>
    </item>
    <item>
      <title>Re: Matching with related dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Matching-with-related-dates/m-p/322490#M71345</link>
      <description>&lt;P&gt;My apologies. They are both datetime20.&lt;/P&gt;</description>
      <pubDate>Wed, 04 Jan 2017 20:39:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Matching-with-related-dates/m-p/322490#M71345</guid>
      <dc:creator>SThomas</dc:creator>
      <dc:date>2017-01-04T20:39:19Z</dc:date>
    </item>
    <item>
      <title>Re: Matching with related dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Matching-with-related-dates/m-p/322492#M71346</link>
      <description>&lt;P&gt;Assuming both have1 and have2 are sorted by id/date, then this interleaving operation will work:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have2 (in=in2 rename=(date2=date1))
      have1 (in=in1);
  by id date1;

  date2=ifn(in1 and lag(in2) and first.id=0,lag(date1),.);
  if in1;
  format date1 date2 datetime25.6;
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;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Notes:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&amp;nbsp;In the SET statement HAVE2 must precede HAVE1&amp;nbsp; (for when dates are exactly tied)&lt;/LI&gt;
&lt;LI&gt;The IFN function tests whether the record-in-hand is from have1, and is immediately preceded by&amp;nbsp;a HAVE2 record for the same id.&amp;nbsp; If true then the prior date1 value is really a date2 var that has an earlier date.&lt;/LI&gt;
&lt;LI&gt;The IFN&amp;nbsp;ALWAYS updates the lag queues, even if the lagged value is not returned by IFN.&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Wed, 04 Jan 2017 20:42:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Matching-with-related-dates/m-p/322492#M71346</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-01-04T20:42:25Z</dc:date>
    </item>
  </channel>
</rss>

