<?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: pick the closest prior date in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/pick-the-closest-prior-date/m-p/285408#M58343</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please see below a sql solution:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;create table want as&lt;BR /&gt;select a.*, cdate from have1 a&lt;BR /&gt;left join have2 b&lt;BR /&gt;on cdate&amp;lt;date&lt;BR /&gt;group by date&lt;BR /&gt;having (date-cdate)=min(date-cdate)&lt;BR /&gt;order by date;&lt;BR /&gt;quit;&lt;/P&gt;</description>
    <pubDate>Mon, 18 Jul 2016 23:55:00 GMT</pubDate>
    <dc:creator>stat_sas</dc:creator>
    <dc:date>2016-07-18T23:55:00Z</dc:date>
    <item>
      <title>pick the closest prior date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/pick-the-closest-prior-date/m-p/285395#M58340</link>
      <description>&lt;P&gt;Dear All,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have two datesets, one is have1 dataset, which has id and date. and another dataset is have2 which has id and cdate. i need to pick the closest prior cdate to date. here is sample to better understand.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;here is my try:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;create table want as&lt;BR /&gt;select a.*&lt;BR /&gt;from have1 as a left join have2 as b on a.id=b.id and a.date &amp;gt;= b.date&lt;BR /&gt;group by a.id;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have1;&lt;BR /&gt;input id date ;&lt;BR /&gt;100 18SEP2013 &lt;BR /&gt;100 25SEP2013&lt;BR /&gt;100 04OCT2013 &lt;BR /&gt;100 11OCT2013 &lt;BR /&gt;100 25OCT2013 &lt;BR /&gt;100 08NOV2013 &lt;BR /&gt;100 22NOV2013 &lt;BR /&gt;100 06DEC2013 &lt;BR /&gt;100 20DEC2013 &lt;BR /&gt;100 08JAN2014 &lt;BR /&gt;100 17JAN2014 &lt;BR /&gt;100 31JAN2014 &lt;BR /&gt;100 17FEB2014 &lt;BR /&gt;100 28FEB2014 &lt;BR /&gt;100 14MAR2014 &lt;BR /&gt;100 28MAR2014 &lt;BR /&gt;100 04APR2014 &lt;BR /&gt;100 02MAY2014 &lt;BR /&gt;;;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data have2;&lt;BR /&gt;input id cdate;&lt;BR /&gt;100 04OCT2013&lt;BR /&gt;100 07OCT2013&lt;BR /&gt;100 09OCT2013&lt;BR /&gt;100 11OCT2013&lt;BR /&gt;100 14OCT2013&lt;BR /&gt;100 16OCT2013&lt;BR /&gt;100 18OCT2013&lt;BR /&gt;100 21OCT2013&lt;BR /&gt;100 23OCT2013&lt;BR /&gt;100 25OCT2013&lt;BR /&gt;100 28OCT2013&lt;BR /&gt;100 30OCT2013&lt;BR /&gt;100 01NOV2013&lt;BR /&gt;100 04NOV2013&lt;BR /&gt;100 06NOV2013&lt;BR /&gt;100 08NOV2013&lt;BR /&gt;100 11NOV2013&lt;BR /&gt;100 13NOV2013&lt;BR /&gt;100 15NOV2013&lt;BR /&gt;100 18NOV2013&lt;BR /&gt;100 20NOV2013&lt;BR /&gt;100 22NOV2013&lt;BR /&gt;100 25NOV2013&lt;BR /&gt;100 27NOV2013&lt;BR /&gt;100 29NOV2013&lt;BR /&gt;100 02DEC2013&lt;BR /&gt;100 04DEC2013&lt;BR /&gt;100 06DEC2013&lt;BR /&gt;100 08DEC2013&lt;BR /&gt;100 11DEC2013&lt;BR /&gt;100 13DEC2013&lt;BR /&gt;100 16DEC2013&lt;BR /&gt;100 18DEC2013&lt;BR /&gt;100 20DEC2013&lt;BR /&gt;100 23DEC2013&lt;BR /&gt;100 26DEC2013&lt;BR /&gt;100 28DEC2013&lt;BR /&gt;100 30DEC2013&lt;BR /&gt;100 02JAN2014&lt;BR /&gt;100 04JAN2014&lt;BR /&gt;100 06JAN2014&lt;BR /&gt;100 08JAN2014&lt;BR /&gt;100 10JAN2014&lt;BR /&gt;100 13JAN2014&lt;BR /&gt;100 15JAN2014&lt;BR /&gt;100 17JAN2014&lt;BR /&gt;100 20JAN2014&lt;BR /&gt;100 22JAN2014&lt;BR /&gt;100 24JAN2014&lt;BR /&gt;100 27JAN2014&lt;BR /&gt;100 29JAN2014&lt;BR /&gt;100 31JAN2014&lt;BR /&gt;100 03FEB2014&lt;BR /&gt;100 05FEB2014&lt;BR /&gt;100 07FEB2014&lt;BR /&gt;100 10FEB2014&lt;BR /&gt;100 12FEB2014&lt;BR /&gt;100 14FEB2014&lt;BR /&gt;100 17FEB2014&lt;BR /&gt;100 19FEB2014&lt;BR /&gt;100 21FEB2014&lt;BR /&gt;100 24FEB2014&lt;BR /&gt;100 26FEB2014&lt;BR /&gt;100 28FEB2014&lt;BR /&gt;100 03MAR2014&lt;BR /&gt;100 05MAR2014&lt;BR /&gt;100 07MAR2014&lt;BR /&gt;100 10MAR2014&lt;BR /&gt;100 12MAR2014&lt;BR /&gt;100 14MAR2014&lt;BR /&gt;100 17MAR2014&lt;BR /&gt;100 19MAR2014&lt;BR /&gt;100 21MAR2014&lt;BR /&gt;100 24MAR2014&lt;BR /&gt;100 26MAR2014&lt;BR /&gt;100 28MAR2014&lt;BR /&gt;100 31MAR2014&lt;BR /&gt;100 02APR2014&lt;BR /&gt;;;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;input id date cdate;&lt;BR /&gt;100 18SEP2013 .&lt;BR /&gt;100 25SEP2013 .&lt;BR /&gt;100 04OCT2013 .&lt;BR /&gt;100 11OCT2013 09OCT2013&lt;BR /&gt;100 25OCT2013 23OCT2013&lt;BR /&gt;100 08NOV2013 06NOV2013&lt;BR /&gt;100 22NOV2013 20NOV2013&lt;BR /&gt;100 06DEC2013 04DEC2013&lt;BR /&gt;100 20DEC2013 18DEC2013&lt;BR /&gt;100 08JAN2014 06JAN2014&lt;BR /&gt;100 17JAN2014 15JAN2014&lt;BR /&gt;100 31JAN2014 29JAN2014&lt;BR /&gt;100 17FEB2014 14FEB2014&lt;BR /&gt;100 28FEB2014 26FEB2014&lt;BR /&gt;100 14MAR2014 12MAR2014&lt;BR /&gt;100 28MAR2014 26MAR2014&lt;BR /&gt;100 04APR2014 02APR2014&lt;BR /&gt;100 02MAY2014 02APR2014&lt;BR /&gt;;;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;P&gt;Sam&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jul 2016 22:35:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/pick-the-closest-prior-date/m-p/285395#M58340</guid>
      <dc:creator>sam369</dc:creator>
      <dc:date>2016-07-18T22:35:55Z</dc:date>
    </item>
    <item>
      <title>Re: pick the closest prior date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/pick-the-closest-prior-date/m-p/285402#M58341</link>
      <description>&lt;P&gt;I'm sure SQL can handle this, but I find it easier to use a DATA step. &amp;nbsp;Assuming both data sets are sorted:&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=(cdate=date) in=in2)&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;SPAN&gt;have1 (in=in1);&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;by id date;&lt;/P&gt;
&lt;P&gt;if first.id then cdate=.;&lt;/P&gt;
&lt;P&gt;retain cdate;&lt;/P&gt;
&lt;P&gt;if in1 then output;&lt;/P&gt;
&lt;P&gt;if in2 then cdate=date;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If both data sets contain the same date, this program selects that date as CDATE. &amp;nbsp;If that's not right and CDATE must be prior to DATE, you would need to switch the order of the data sets in the SET statement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;set have1 (in=in1)&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; have2 (rename=(cdate=date) in=in2);&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jul 2016 23:01:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/pick-the-closest-prior-date/m-p/285402#M58341</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-07-18T23:01:11Z</dc:date>
    </item>
    <item>
      <title>Re: pick the closest prior date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/pick-the-closest-prior-date/m-p/285406#M58342</link>
      <description>&lt;P&gt;Thank you astounding,&lt;/P&gt;
&lt;P&gt;But the ouput results are not what i expected, according to your suggested code. same date is getting for cdate and date. i am looking for closest prior date&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;for record 100 date from have1 11oct2013 and cdate should be priod date of date that means 09oct2013&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;P&gt;Sam&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jul 2016 23:15:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/pick-the-closest-prior-date/m-p/285406#M58342</guid>
      <dc:creator>sam369</dc:creator>
      <dc:date>2016-07-18T23:15:37Z</dc:date>
    </item>
    <item>
      <title>Re: pick the closest prior date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/pick-the-closest-prior-date/m-p/285408#M58343</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please see below a sql solution:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;create table want as&lt;BR /&gt;select a.*, cdate from have1 a&lt;BR /&gt;left join have2 b&lt;BR /&gt;on cdate&amp;lt;date&lt;BR /&gt;group by date&lt;BR /&gt;having (date-cdate)=min(date-cdate)&lt;BR /&gt;order by date;&lt;BR /&gt;quit;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jul 2016 23:55:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/pick-the-closest-prior-date/m-p/285408#M58343</guid>
      <dc:creator>stat_sas</dc:creator>
      <dc:date>2016-07-18T23:55:00Z</dc:date>
    </item>
    <item>
      <title>Re: pick the closest prior date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/pick-the-closest-prior-date/m-p/285409#M58344</link>
      <description>&lt;P&gt;Thank you both!!!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;P&gt;Sam&lt;/P&gt;</description>
      <pubDate>Tue, 19 Jul 2016 00:01:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/pick-the-closest-prior-date/m-p/285409#M58344</guid>
      <dc:creator>sam369</dc:creator>
      <dc:date>2016-07-19T00:01:59Z</dc:date>
    </item>
    <item>
      <title>Re: pick the closest prior date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/pick-the-closest-prior-date/m-p/285410#M58345</link>
      <description>&lt;P&gt;Hmmmm ....&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Did you try switching the order in the SET statement, like I mentioned at the bottom of my post?&lt;/P&gt;</description>
      <pubDate>Tue, 19 Jul 2016 00:18:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/pick-the-closest-prior-date/m-p/285410#M58345</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-07-19T00:18:14Z</dc:date>
    </item>
    <item>
      <title>Re: pick the closest prior date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/pick-the-closest-prior-date/m-p/285413#M58346</link>
      <description>&lt;P&gt;Hi Astounding,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Intially i did not look that part, later i checked and it worked !!! thank you so much for the help&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;P&gt;Sam&lt;/P&gt;</description>
      <pubDate>Tue, 19 Jul 2016 00:23:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/pick-the-closest-prior-date/m-p/285413#M58346</guid>
      <dc:creator>sam369</dc:creator>
      <dc:date>2016-07-19T00:23:35Z</dc:date>
    </item>
  </channel>
</rss>

