<?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: Conducting a fuzzy merge on dates while specifying a minimum difference between dates in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Conducting-a-fuzzy-merge-on-dates-while-specifying-a-minimum/m-p/449514#M283601</link>
    <description>&lt;P&gt;EDIT:&amp;nbsp; I JUST SAW THIS HAS BEEN DISCUSSED AND SOLVED IN ANOTHER THREAD.&amp;nbsp; DON'T DOUBLE POST UP LIKE THAT.&amp;nbsp; I'LL LEAVE THIS ANSWER IN CASE IT APPLIES TO SOMEONE ELSE'S PROBLEM.&lt;/P&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;You could do it less elegantly with a&amp;nbsp;subquery.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You don't post any sample data and not&amp;nbsp;real good guidance but given data like&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data aa;
input id one mydate:mmddyy10. something:$6.;
format mydate yymmdd10.;
datalines;
111 100 6/1/2014  yergle
112 200 7/2/2014  omnipt
113 500 8/4/2014  weezle
;
run;


Data bb;
input id one mydate:mmddyy10. var1:$3. var2:$3. var3:$3.;
format mydate yymmdd10.;
datalines;
111 100 7/1/2014  AAA bbb ccc
111 200 5/30/2014  bbb ccc ddd
111 500 5/15/2014  ddd eee fff
112 100 7/3/2014  AAA xxx sss
112 200 7/4/2014  AAA zzz qqq
112 500 7/5/2014  ABC vvv bbb
113 100 6/1/2014  AAA nnn mmm
113 200 6/2/2014  AAA eee ttt
113 500 6/4/2014  ABC fff ggg
;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;if I understand correctly you would want data from BB records with id of 111 and var values of bbb ccc ddd and 113 ABC fff ggg&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;LI-WRAPPER&gt;&lt;/LI-WRAPPER&gt;&lt;/P&gt;
&lt;P&gt;A subquery could find the closest record and then you can join back.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	select bb.*, aa.something
	from (
		select bb.id, max(bb.mydate) as myccdate
		from bb 
		inner join aa on bb.id = aa.id
		where bb.mydate &amp;lt;= aa.mydate
		group by bb.id
		) as cc
	left join bb on bb.id = cc.id and bb.mydate = cc.myccdate
	left join aa on bb.id = aa.id;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp; This produces&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;                                        The SAS System     
                         id       one      mydate  var1  var2  var3  something
                   
                        111       200  2014-05-30  bbb   ccc   ddd   yergle
                        113       500  2014-06-04  ABC   fff   ggg   weezle


&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;Maybe this is completely off base.&amp;nbsp; Maybe it will be useful.&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 28 Mar 2018 21:57:19 GMT</pubDate>
    <dc:creator>HB</dc:creator>
    <dc:date>2018-03-28T21:57:19Z</dc:date>
    <item>
      <title>Conducting a fuzzy merge on dates while specifying a minimum difference between dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conducting-a-fuzzy-merge-on-dates-while-specifying-a-minimum/m-p/449425#M283598</link>
      <description>&lt;P&gt;I have been using the following syntax to conduct a fuzzy merge on dates between two tables:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;proc&lt;/SPAN&gt; &lt;SPAN class="token procnames"&gt;sql&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
  create &lt;SPAN class="token statement"&gt;table&lt;/SPAN&gt; want as
    &lt;SPAN class="token statement"&gt;select&lt;/SPAN&gt; 
      a&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;
      b&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;start as r_start &lt;SPAN class="token procnames"&gt;format&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;date9&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;
      b&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;end   as r_end &lt;SPAN class="token procnames"&gt;format&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;date9&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;
      b&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;var1&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;
      b&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;var2&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;
      b&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;var3
    &lt;SPAN class="token keyword"&gt;from&lt;/SPAN&gt; 
      a &lt;SPAN class="token function"&gt;left&lt;/SPAN&gt; join b
        on a&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token keyword"&gt;id&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;b&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token keyword"&gt;id&lt;/SPAN&gt;
      &lt;SPAN class="token keyword"&gt;group&lt;/SPAN&gt; &lt;SPAN class="token statement"&gt;by&lt;/SPAN&gt; a&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token keyword"&gt;id&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; a&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;start
      having &lt;SPAN class="token function"&gt;min&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;a&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;start&lt;SPAN class="token operator"&gt;-&lt;/SPAN&gt;b&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;start&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;a&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;start&lt;SPAN class="token operator"&gt;-&lt;/SPAN&gt;b&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;start&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;
    &lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;quit&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;As an additional step, I need to specify that the minimum value of (a.start-b.start) is the minimum value that is &amp;gt;= 0. Essentially what I am trying to do is identify the closest date to &lt;STRONG&gt;a.start&lt;/STRONG&gt; in table b, but with the added caveat that &lt;STRONG&gt;b.start&lt;/STRONG&gt; has to have occurred &lt;U&gt;before or on the same day&lt;/U&gt; as &lt;STRONG&gt;a.start&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Mar 2018 16:45:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conducting-a-fuzzy-merge-on-dates-while-specifying-a-minimum/m-p/449425#M283598</guid>
      <dc:creator>djohn051</dc:creator>
      <dc:date>2018-03-28T16:45:11Z</dc:date>
    </item>
    <item>
      <title>Re: Conducting a fuzzy merge on dates while specifying a minimum difference between dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conducting-a-fuzzy-merge-on-dates-while-specifying-a-minimum/m-p/449496#M283600</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/171133"&gt;@djohn051&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Couldn't you just filter out these cases during the joinl?&lt;/P&gt;
&lt;PRE&gt;on a.id=b.id and (a.start-b.start) &amp;gt;=0&lt;/PRE&gt;
&lt;P&gt;Above will also filter cases where b.start is missing because there was no matching record in table with alias B (so actually: you could also go for an inner join which would perform better). If you want to include non matching records in your final result then modify the join condition to:&lt;/P&gt;
&lt;PRE&gt;on a.id=b.id and (a.start-b.start &amp;gt;0 or b.start is null)&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Mar 2018 20:45:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conducting-a-fuzzy-merge-on-dates-while-specifying-a-minimum/m-p/449496#M283600</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2018-03-28T20:45:24Z</dc:date>
    </item>
    <item>
      <title>Re: Conducting a fuzzy merge on dates while specifying a minimum difference between dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conducting-a-fuzzy-merge-on-dates-while-specifying-a-minimum/m-p/449514#M283601</link>
      <description>&lt;P&gt;EDIT:&amp;nbsp; I JUST SAW THIS HAS BEEN DISCUSSED AND SOLVED IN ANOTHER THREAD.&amp;nbsp; DON'T DOUBLE POST UP LIKE THAT.&amp;nbsp; I'LL LEAVE THIS ANSWER IN CASE IT APPLIES TO SOMEONE ELSE'S PROBLEM.&lt;/P&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;You could do it less elegantly with a&amp;nbsp;subquery.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You don't post any sample data and not&amp;nbsp;real good guidance but given data like&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data aa;
input id one mydate:mmddyy10. something:$6.;
format mydate yymmdd10.;
datalines;
111 100 6/1/2014  yergle
112 200 7/2/2014  omnipt
113 500 8/4/2014  weezle
;
run;


Data bb;
input id one mydate:mmddyy10. var1:$3. var2:$3. var3:$3.;
format mydate yymmdd10.;
datalines;
111 100 7/1/2014  AAA bbb ccc
111 200 5/30/2014  bbb ccc ddd
111 500 5/15/2014  ddd eee fff
112 100 7/3/2014  AAA xxx sss
112 200 7/4/2014  AAA zzz qqq
112 500 7/5/2014  ABC vvv bbb
113 100 6/1/2014  AAA nnn mmm
113 200 6/2/2014  AAA eee ttt
113 500 6/4/2014  ABC fff ggg
;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;if I understand correctly you would want data from BB records with id of 111 and var values of bbb ccc ddd and 113 ABC fff ggg&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;LI-WRAPPER&gt;&lt;/LI-WRAPPER&gt;&lt;/P&gt;
&lt;P&gt;A subquery could find the closest record and then you can join back.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	select bb.*, aa.something
	from (
		select bb.id, max(bb.mydate) as myccdate
		from bb 
		inner join aa on bb.id = aa.id
		where bb.mydate &amp;lt;= aa.mydate
		group by bb.id
		) as cc
	left join bb on bb.id = cc.id and bb.mydate = cc.myccdate
	left join aa on bb.id = aa.id;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp; This produces&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;                                        The SAS System     
                         id       one      mydate  var1  var2  var3  something
                   
                        111       200  2014-05-30  bbb   ccc   ddd   yergle
                        113       500  2014-06-04  ABC   fff   ggg   weezle


&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;Maybe this is completely off base.&amp;nbsp; Maybe it will be useful.&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Mar 2018 21:57:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conducting-a-fuzzy-merge-on-dates-while-specifying-a-minimum/m-p/449514#M283601</guid>
      <dc:creator>HB</dc:creator>
      <dc:date>2018-03-28T21:57:19Z</dc:date>
    </item>
    <item>
      <title>Re: Conducting a fuzzy merge on dates while specifying a minimum difference between dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conducting-a-fuzzy-merge-on-dates-while-specifying-a-minimum/m-p/449521#M283602</link>
      <description>&lt;P&gt;Note that within a a.start group, the value of a.start is constant. So the condition&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;SPAN class="token function"&gt;min&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;a&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;start&lt;SPAN class="token operator"&gt;-&lt;/SPAN&gt;b&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;start&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;a&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;start&lt;SPAN class="token operator"&gt;-&lt;/SPAN&gt;b&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;start&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token punctuation"&gt;is equivalent to &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;SPAN class="token punctuation"&gt;min(-b.start) = -b.start&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token punctuation"&gt;which is the same as &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;SPAN class="token punctuation"&gt;max(b.start) = b.start&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token punctuation"&gt;you could thus use the query&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table want as
    select 
      a.*,
      b.start as r_start format=date9.,
      b.end   as r_end format=date9.,
      b.var1,
      b.var2,
      b.var3
    from 
      a left join b
        on a.id=b.id and a.start &amp;gt;= b.start
      group by a.id, a.start
      having max(b.start) = b.start
    ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Mar 2018 22:04:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conducting-a-fuzzy-merge-on-dates-while-specifying-a-minimum/m-p/449521#M283602</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-03-28T22:04:47Z</dc:date>
    </item>
    <item>
      <title>Re: Conducting a fuzzy merge on dates while specifying a minimum difference between dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conducting-a-fuzzy-merge-on-dates-while-specifying-a-minimum/m-p/449692#M283603</link>
      <description>&lt;P&gt;This worked perfectly. I was too focused on trying to integrate&amp;nbsp;everything on the having line that I didn't think to remove all of the cases where time b &amp;gt; time a during the actual join.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 29 Mar 2018 15:07:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conducting-a-fuzzy-merge-on-dates-while-specifying-a-minimum/m-p/449692#M283603</guid>
      <dc:creator>djohn051</dc:creator>
      <dc:date>2018-03-29T15:07:43Z</dc:date>
    </item>
  </channel>
</rss>

