<?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: Join/merge with dates in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Join-merge-with-dates/m-p/165626#M300605</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I wold use datdif in that case. Provided that you want the statement to be that b.date occurred within 1 to 31 days after a.date the below code would work, if you wanted before you could either switch a.date and b.date in the statement or change the between to -31 and -1.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;the having statement would be&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;having (datdif(a.date,b.date,'act/act')) between 1 and 31&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 07 Jan 2014 16:50:28 GMT</pubDate>
    <dc:creator>overmar</dc:creator>
    <dc:date>2014-01-07T16:50:28Z</dc:date>
    <item>
      <title>Join/merge with dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-merge-with-dates/m-p/165616#M300595</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I hope someone can give me advice how to best merge/join this two sets.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The first data set contains two variables (id and date) Only one unique id.&lt;/P&gt;&lt;P&gt;id&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; date&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1Jul2010&lt;/P&gt;&lt;P&gt;2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3Aug2011&lt;/P&gt;&lt;P&gt;3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2Feb2012&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The second dataset are almost the same but the same id can have more than one date.&lt;/P&gt;&lt;P&gt;id&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; date&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 7Jul2010&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3Aug2010&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 26Jun2010&lt;/P&gt;&lt;P&gt;2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4Aug2011&lt;/P&gt;&lt;P&gt;3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1Feb2012&lt;/P&gt;&lt;P&gt;3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 8Feb2012&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I now want to join/merge table one with table wto &lt;STRONG&gt;but&lt;/STRONG&gt; only the date from table 2 closest in time to the date in table should be selected&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Like this.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;id&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; date&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; date(from table 2)&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1Jul2010&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 7Jul2010&lt;/P&gt;&lt;P&gt;2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3Aug2011&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4Aug2011&lt;/P&gt;&lt;P&gt;3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2Feb2012&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1Feb2012&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have done this by using first/last options but I hope it could be easier to do this like including a if/where clause in the merge&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;Thomas&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 06 Dec 2013 01:07:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-merge-with-dates/m-p/165616#M300595</guid>
      <dc:creator>bollibompa</dc:creator>
      <dc:date>2013-12-06T01:07:58Z</dc:date>
    </item>
    <item>
      <title>Re: Join/merge with dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-merge-with-dates/m-p/165617#M300596</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;SQL is well suited to this type of "merge".&amp;nbsp; I'm not sure my use of SQL is the best but it seems to work except that jun26 is selected for ID=1.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;DIV style="font-family: Courier New; font-size: 11pt;"&gt;&lt;STRONG style="color: #000080; background-color: #ffffff;"&gt;data&lt;/STRONG&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt; one;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; background-color: #ffffff;"&gt;input&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt; id date:&lt;/SPAN&gt;&lt;SPAN style="color: #008080; background-color: #ffffff;"&gt;date.&lt;/SPAN&gt;; &lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; background-color: #ffffff;"&gt;format&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt; date &lt;/SPAN&gt;&lt;SPAN style="color: #008080; background-color: #ffffff;"&gt;date.&lt;/SPAN&gt;; &lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; background-color: #ffffff;"&gt;cards&lt;/SPAN&gt;; &lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffc0;"&gt;1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1Jul2010&lt;BR /&gt;2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3Aug2011&lt;BR /&gt;3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2Feb2012&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;;;;;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;STRONG style="color: #000080; background-color: #ffffff;"&gt;run&lt;/STRONG&gt;; &lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;STRONG style="color: #000080; background-color: #ffffff;"&gt;data&lt;/STRONG&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt; two;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; background-color: #ffffff;"&gt;input&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt; id date:&lt;/SPAN&gt;&lt;SPAN style="color: #008080; background-color: #ffffff;"&gt;date.&lt;/SPAN&gt;; &lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; background-color: #ffffff;"&gt;format&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt; date &lt;/SPAN&gt;&lt;SPAN style="color: #008080; background-color: #ffffff;"&gt;date.&lt;/SPAN&gt;; &lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; background-color: #ffffff;"&gt;cards&lt;/SPAN&gt;; &lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffc0;"&gt;1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 7Jul2010&lt;BR /&gt;1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3Aug2010&lt;BR /&gt;1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 26Jun2010&lt;BR /&gt;2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4Aug2011&lt;BR /&gt;3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1Feb2012&lt;BR /&gt;3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 8Feb2012&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;;;;;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;STRONG style="color: #000080; background-color: #ffffff;"&gt;run&lt;/STRONG&gt;; &lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;STRONG style="color: #000080; background-color: #ffffff;"&gt;proc&lt;/STRONG&gt; &lt;STRONG style="color: #000080; background-color: #ffffff;"&gt;sql&lt;/STRONG&gt;; &lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; background-color: #ffffff;"&gt;select&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt; a.id,a.date,b.date &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; background-color: #ffffff;"&gt;as&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt; date2 &lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN style="color: #008000; background-color: #ffffff;"&gt;/*&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,min(abs(a.date-b.date)),abs(a.date-b.date)*/&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; background-color: #ffffff;"&gt;from&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt; one &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; background-color: #ffffff;"&gt;as&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt; a join two &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; background-color: #ffffff;"&gt;as&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt; b&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; background-color: #ffffff;"&gt;on&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt; a.id eq b.id &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; background-color: #ffffff;"&gt;group&lt;/SPAN&gt; &lt;SPAN style="color: #0000ff; background-color: #ffffff;"&gt;by&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt; a.id&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; background-color: #ffffff;"&gt;having&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt; abs(a.date-b.date) eq min(abs(a.date-b.date))&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;STRONG style="color: #000080; background-color: #ffffff;"&gt;quit&lt;/STRONG&gt;; &lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;STRONG style="color: #000080; background-color: #ffffff;"&gt;run&lt;/STRONG&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 06 Dec 2013 02:08:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-merge-with-dates/m-p/165617#M300596</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2013-12-06T02:08:17Z</dc:date>
    </item>
    <item>
      <title>Re: Join/merge with dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-merge-with-dates/m-p/165618#M300597</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Or as a datastep:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data want;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;do until (last.id);&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; merge one (rename=date=date1) two;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; by id;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if first.id or (range(date2, date1) &amp;gt; range(date, date1)) then date2 = date;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;format date2 date9.;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;drop date;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;run;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This simple merge is more efficient than SQL but relies on 1) both datasets are sorted by &lt;STRONG&gt;id&lt;/STRONG&gt; and &lt;STRONG&gt;date&lt;/STRONG&gt; and 2) dataset &lt;STRONG&gt;one&lt;/STRONG&gt; contains a single &lt;STRONG&gt;date&lt;/STRONG&gt; per &lt;STRONG&gt;id.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;PG&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 06 Dec 2013 03:28:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-merge-with-dates/m-p/165618#M300597</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2013-12-06T03:28:19Z</dc:date>
    </item>
    <item>
      <title>Re: Join/merge with dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-merge-with-dates/m-p/165619#M300598</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you both for brilliant answers!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;An attendant question:&lt;/P&gt;&lt;P&gt;The range function in the data step seems to be powerful but have not found so much information about it on the internet.&lt;/P&gt;&lt;P&gt;I can see that the result is correct but I dont get the second part of the code&amp;nbsp; "&amp;gt; range(date, date1)"?.&lt;/P&gt;&lt;P&gt;It is possible to quick explain the syntax of this function? I am trying to add an extra condition where the date (date in data set "two") must be more than +1 day and less than 31 days than the date in&amp;nbsp; data set "one"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you for all help!&lt;/P&gt;&lt;P&gt;/Thomas&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;What exactly doI have googled the range function and I would likte to leran more about&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 08 Dec 2013 21:10:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-merge-with-dates/m-p/165619#M300598</guid>
      <dc:creator>bollibompa</dc:creator>
      <dc:date>2013-12-08T21:10:45Z</dc:date>
    </item>
    <item>
      <title>Re: Join/merge with dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-merge-with-dates/m-p/165620#M300599</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;if you Google: range function sas I think your first hit will be:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000245938.htm" title="https://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000245938.htm"&gt;SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition&lt;/A&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 08 Dec 2013 21:29:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-merge-with-dates/m-p/165620#M300599</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2013-12-08T21:29:13Z</dc:date>
    </item>
    <item>
      <title>Re: Join/merge with dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-merge-with-dates/m-p/165621#M300600</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank for this link!&lt;/P&gt;&lt;P&gt;I have now read about the range function and the code works fine. But one thing I really cant understand is that you create the variable date2 &lt;STRONG&gt;after&lt;/STRONG&gt; the range statement and date2 is used in the range statement. This may be fundamental data step programming but I cant really understand how this works.I am not sure I understand why the first range statement&amp;nbsp; (range(date2,date1) should be bigger than the other.&lt;/P&gt;&lt;P&gt;I feel a little bit stupid here but am a little bit frustrated I cant understand why it works.&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;Thomas&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data want;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;do until (last.id);&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; merge one (rename=date=date1) two;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; by id;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if first.id or (range(date2, date1) &amp;gt; range(date, date1)) then date2 = date;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;format date2 date9.;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;drop date;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;run;&lt;/STRONG&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 09 Dec 2013 10:05:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-merge-with-dates/m-p/165621#M300600</guid>
      <dc:creator>bollibompa</dc:creator>
      <dc:date>2013-12-09T10:05:43Z</dc:date>
    </item>
    <item>
      <title>Re: Join/merge with dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-merge-with-dates/m-p/165622#M300601</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You said: "&lt;SPAN style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt; I cant really understand how this works.I am not sure I understand why the first range statement&amp;nbsp; (range(date2,date1) should be bigger than the other.&lt;/SPAN&gt;"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It shouldn't but, if you look at the statement, the first part of the expression (i.e., first.id) will be true.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 09 Dec 2013 15:44:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-merge-with-dates/m-p/165622#M300601</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2013-12-09T15:44:48Z</dc:date>
    </item>
    <item>
      <title>Re: Join/merge with dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-merge-with-dates/m-p/165623#M300602</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I used the &lt;STRONG&gt;range&lt;/STRONG&gt; function precisely for the fact that it works just fine, without complaining, when some of its arguments are missing. If &lt;STRONG&gt;A&lt;/STRONG&gt; is missing, &lt;STRONG&gt;RANGE(A, B)&lt;/STRONG&gt; is the same of &lt;STRONG&gt;RANGE(B)&lt;/STRONG&gt; which is 0. If both &lt;STRONG&gt;A&lt;/STRONG&gt; and &lt;STRONG&gt;B&lt;/STRONG&gt; are missing then &lt;STRONG&gt;RANGE(A, B)&lt;/STRONG&gt; will return a missing value. As Art pointed, the &lt;STRONG&gt;first.id&lt;/STRONG&gt; condition is important to the logic of the program. I couldn't find a way to get rid of it.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;PG&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 09 Dec 2013 16:23:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-merge-with-dates/m-p/165623#M300602</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2013-12-09T16:23:11Z</dc:date>
    </item>
    <item>
      <title>Re: Join/merge with dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-merge-with-dates/m-p/165624#M300603</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;An additional question,&lt;/P&gt;&lt;P&gt;I have the same dataset and I now want to select the date closest in time BUT the the code should only select if the date (in dataset two) is between 1 and 31 days from the date in dataset one.&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Something like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="color: #000080; background-color: #ffffff;"&gt;proc&lt;/STRONG&gt; &lt;STRONG style="color: #000080; background-color: #ffffff;"&gt;sql&lt;/STRONG&gt;; &lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; background-color: #ffffff;"&gt;select&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt; a.id,a.date,b.date &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; background-color: #ffffff;"&gt;as&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt; date2 &lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; background-color: #ffffff;"&gt;from&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt; one &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; background-color: #ffffff;"&gt;as&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt; a join two &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; background-color: #ffffff;"&gt;as&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt; b&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; background-color: #ffffff;"&gt;on&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt; a.id eq b.id &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; background-color: #ffffff;"&gt;group&lt;/SPAN&gt; &lt;SPAN style="color: #0000ff; background-color: #ffffff;"&gt;by&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt; a.id&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; background-color: #ffffff;"&gt;having&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt; (b.date-a.date) eq min(b.date-a.date) /* and something more here that the timedifference must me between 1 and 31 days*/&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 07 Jan 2014 15:37:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-merge-with-dates/m-p/165624#M300603</guid>
      <dc:creator>bollibompa</dc:creator>
      <dc:date>2014-01-07T15:37:17Z</dc:date>
    </item>
    <item>
      <title>Re: Join/merge with dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-merge-with-dates/m-p/165625#M300604</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Because the MERGE statement is nested within the DO loop the values of DATE2 will be "retained" from one observation to the next within the current value of ID.&amp;nbsp; So DATE2 will keep its value until it is reset by the conditional assignment statement.&amp;nbsp; When the DO loop ends and the data step goes to a new iteration DATE2 will be set missing again since it is not actually referenced in a RETAIN statement.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 07 Jan 2014 16:45:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-merge-with-dates/m-p/165625#M300604</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2014-01-07T16:45:51Z</dc:date>
    </item>
    <item>
      <title>Re: Join/merge with dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-merge-with-dates/m-p/165626#M300605</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I wold use datdif in that case. Provided that you want the statement to be that b.date occurred within 1 to 31 days after a.date the below code would work, if you wanted before you could either switch a.date and b.date in the statement or change the between to -31 and -1.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;the having statement would be&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;having (datdif(a.date,b.date,'act/act')) between 1 and 31&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 07 Jan 2014 16:50:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-merge-with-dates/m-p/165626#M300605</guid>
      <dc:creator>overmar</dc:creator>
      <dc:date>2014-01-07T16:50:28Z</dc:date>
    </item>
    <item>
      <title>Re: Join/merge with dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-merge-with-dates/m-p/165627#M300606</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks for reply!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I tried&amp;nbsp; &lt;EM&gt;having (datdif(a.date,b.date,'act/act')) between 1 and 31 &lt;/EM&gt;and it works fine, however I further only want to keep the mininum date&amp;nbsp; (if there are more dates in table two. So I added this statement:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff; font-size: 12pt; font-family: Courier New;"&gt;having&lt;/SPAN&gt;&lt;SPAN style="font-size: 12pt; font-family: Courier New;"&gt; datdif(a.date,b.date,&lt;/SPAN&gt;&lt;SPAN style="color: #800080; font-size: 12pt; font-family: Courier New;"&gt;'act/act'&lt;/SPAN&gt;&lt;SPAN style="font-size: 12pt; font-family: Courier New;"&gt;)&amp;nbsp; eq min(datdif(a.date,b.date,&lt;/SPAN&gt;&lt;SPAN style="color: #800080; font-size: 12pt; font-family: Courier New;"&gt;'act/act'&lt;/SPAN&gt;&lt;SPAN style="font-size: 12pt; font-family: Courier New;"&gt;)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 12pt; font-family: Courier New;"&gt;but now I have troubles adding the "between 1 and 31" statement. When I do that it only keeps the minimum date of all id:s not grouped by id.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 12pt; font-family: Courier New;"&gt;/Thomas&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 08 Jan 2014 08:48:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-merge-with-dates/m-p/165627#M300606</guid>
      <dc:creator>bollibompa</dc:creator>
      <dc:date>2014-01-08T08:48:19Z</dc:date>
    </item>
    <item>
      <title>Re: Join/merge with dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-merge-with-dates/m-p/165628#M300607</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;SPAN style="color: #000000; font-size: 11pt; background: white; font-family: 'Courier New';"&gt;How about the following one:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="color: navy; font-size: 11pt; background: white; font-family: 'Courier New';"&gt;data&lt;/STRONG&gt;&lt;SPAN style="font-family: 'Courier New'; background: white; color: black; font-size: 11pt;"&gt; both;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &lt;SPAN style="font-family: 'Courier New'; background: white; color: blue; font-size: 11pt;"&gt;merge&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; background: white; color: black; font-size: 11pt;"&gt; one two(rename = (date = date1));&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &lt;SPAN style="font-family: 'Courier New'; background: white; color: blue; font-size: 11pt;"&gt;by&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; background: white; color: black; font-size: 11pt;"&gt; id;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &lt;SPAN style="font-family: 'Courier New'; background: white; color: blue; font-size: 11pt;"&gt;if&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; background: white; color: black; font-size: 11pt;"&gt; first.id &lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; background: white; color: blue; font-size: 11pt;"&gt;then&lt;/SPAN&gt; &lt;SPAN style="font-family: 'Courier New'; background: white; color: blue; font-size: 11pt;"&gt;output&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; background: white; color: black; font-size: 11pt;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="color: navy; font-size: 11pt; background: white; font-family: 'Courier New';"&gt;run&lt;/STRONG&gt;&lt;SPAN style="font-family: 'Courier New'; background: white; color: black; font-size: 11pt;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Courier New'; background: white; color: black; font-size: 11pt;"&gt;-Urvish&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 08 Jan 2014 10:13:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-merge-with-dates/m-p/165628#M300607</guid>
      <dc:creator>UrvishShah</dc:creator>
      <dc:date>2014-01-08T10:13:53Z</dc:date>
    </item>
    <item>
      <title>Re: Join/merge with dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-merge-with-dates/m-p/165629#M300608</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;BR /&gt;I can do this in two step process but it would be nicer if it is possible to perform this in one sql query (for each id only keep the date2 closest in time to date BUT date2 must be in the time range of 1 to 31 days from date)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 12pt; font-family: Courier New;"&gt;Like this but this does not actually work, something is wrong with the "between 1 and 31"&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; create table want &lt;/P&gt;&lt;P&gt;as select&amp;nbsp;&amp;nbsp; a.id,a.date, b.date as date2 &lt;/P&gt;&lt;P&gt;from one as a left join two as b&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; on a.id eq b.id &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; group by a.id&lt;/P&gt;&lt;P&gt;having (datdif(a.date,b.date,'act/act') between 1 and 31) eq min(datdif(a.date,b.date,'act/act'))&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 12pt; font-family: Courier New;"&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 08 Jan 2014 14:01:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-merge-with-dates/m-p/165629#M300608</guid>
      <dc:creator>bollibompa</dc:creator>
      <dc:date>2014-01-08T14:01:07Z</dc:date>
    </item>
    <item>
      <title>Re: Join/merge with dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-merge-with-dates/m-p/165630#M300609</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;SPAN style="color: #000000; font-size: 11pt; background: white; font-family: 'Courier New';"&gt;Here, i think there is no need to use the PROC SQL if you can achive the same output by using Data Step...&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000; font-size: 11pt; background: white; font-family: 'Courier New';"&gt;The below one is modefied code which achives the same output even if the dates are not in proper structure...&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000; font-size: 11pt; background: white; font-family: 'Courier New';"&gt;If you think about the efficiency then Data Step is more efficient in this case as SAS Has to use summary functions in HAVING Clause in order to subset the group which of course leads the SAS to make extra pass through the data...&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="color: navy; font-size: 11pt; background: white; font-family: 'Courier New';"&gt;proc&lt;/STRONG&gt; &lt;STRONG style="color: navy; font-size: 11pt; background: white; font-family: 'Courier New';"&gt;sort&lt;/STRONG&gt; &lt;SPAN style="font-family: 'Courier New'; background: white; color: blue; font-size: 11pt;"&gt;data&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; background: white; color: black; font-size: 11pt;"&gt; = one; &lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; background: white; color: blue; font-size: 11pt;"&gt;by&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; background: white; color: black; font-size: 11pt;"&gt; id date; &lt;/SPAN&gt;&lt;STRONG style="color: navy; font-size: 11pt; background: white; font-family: 'Courier New';"&gt;run&lt;/STRONG&gt;&lt;SPAN style="font-family: 'Courier New'; background: white; color: black; font-size: 11pt;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="color: navy; font-size: 11pt; background: white; font-family: 'Courier New';"&gt;proc&lt;/STRONG&gt; &lt;STRONG style="color: navy; font-size: 11pt; background: white; font-family: 'Courier New';"&gt;sort&lt;/STRONG&gt; &lt;SPAN style="font-family: 'Courier New'; background: white; color: blue; font-size: 11pt;"&gt;data&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; background: white; color: black; font-size: 11pt;"&gt; = two; &lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; background: white; color: blue; font-size: 11pt;"&gt;by&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; background: white; color: black; font-size: 11pt;"&gt; id date; &lt;/SPAN&gt;&lt;STRONG style="color: navy; font-size: 11pt; background: white; font-family: 'Courier New';"&gt;run&lt;/STRONG&gt;&lt;SPAN style="font-family: 'Courier New'; background: white; color: black; font-size: 11pt;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Courier New'; background: white; color: black; font-size: 11pt;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="color: navy; font-size: 11pt; background: white; font-family: 'Courier New';"&gt;data&lt;/STRONG&gt;&lt;SPAN style="font-family: 'Courier New'; background: white; color: black; font-size: 11pt;"&gt; both;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &lt;SPAN style="font-family: 'Courier New'; background: white; color: blue; font-size: 11pt;"&gt;merge&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; background: white; color: black; font-size: 11pt;"&gt; one two(rename = (date = date1));&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &lt;SPAN style="font-family: 'Courier New'; background: white; color: blue; font-size: 11pt;"&gt;by&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; background: white; color: black; font-size: 11pt;"&gt; id;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &lt;SPAN style="font-family: 'Courier New'; background: white; color: blue; font-size: 11pt;"&gt;if&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; background: white; color: black; font-size: 11pt;"&gt; first.id &lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; background: white; color: blue; font-size: 11pt;"&gt;then&lt;/SPAN&gt; &lt;SPAN style="font-family: 'Courier New'; background: white; color: blue; font-size: 11pt;"&gt;output&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; background: white; color: black; font-size: 11pt;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="color: navy; font-size: 11pt; background: white; font-family: 'Courier New';"&gt;run&lt;/STRONG&gt;&lt;SPAN style="font-family: 'Courier New'; background: white; color: black; font-size: 11pt;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Courier New'; background: white; color: black; font-size: 11pt;"&gt;-Urvish&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 09 Jan 2014 06:06:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-merge-with-dates/m-p/165630#M300609</guid>
      <dc:creator>UrvishShah</dc:creator>
      <dc:date>2014-01-09T06:06:26Z</dc:date>
    </item>
    <item>
      <title>Re: Join/merge with dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-merge-with-dates/m-p/165631#M300610</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Ok so the issue was that you actually only wanted one row per line and you wanted the minimum of the values which met your requirements. Data step does a much better job of picking off the first observation, but if you wanted to use SQL and combine everything into one statement the below code will work. It takes the distinct IDs and their date from table one and looks at the difference between a.date and b.date and then outputs all of those which are between 1 and 31 days and it then takes the minimum date and only reports that as date2. Since distinct and group by were used and there are no duplicate values for a.date this code will produce 3 IDs, 3 dates, and the first date for all 3 IDs and Dates which met your criteria.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;create table want&lt;/P&gt;&lt;P&gt;as select distinct&amp;nbsp; a.id,a.date, (min(case when datdif(a.date,b.date,'act/act') between 1 and 31 then b.date else . end)) format=mmddyy10. as date2&lt;/P&gt;&lt;P&gt;from one as a left join two as b on a.id eq b.id&lt;/P&gt;&lt;P&gt;group by a.id;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 09 Jan 2014 16:39:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-merge-with-dates/m-p/165631#M300610</guid>
      <dc:creator>overmar</dc:creator>
      <dc:date>2014-01-09T16:39:42Z</dc:date>
    </item>
    <item>
      <title>Re: Join/merge with dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-merge-with-dates/m-p/165632#M300611</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks!!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 15 Jan 2014 07:59:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-merge-with-dates/m-p/165632#M300611</guid>
      <dc:creator>bollibompa</dc:creator>
      <dc:date>2014-01-15T07:59:00Z</dc:date>
    </item>
  </channel>
</rss>

