<?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 Multiple Multidata hash joins in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Multiple-Multidata-hash-joins/m-p/720016#M223005</link>
    <description>&lt;P&gt;Hello all -&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm looking for some help on how to do multiple multidata hash joins in tandem. Essentially, I'm trying to do two proc sql left joins in a hash join instead.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here are the tables I'm dealing with:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;HAVE_BIG:&lt;/STRONG&gt; RECORDS (NAME, RECORD_ID, DATE_OF_SERVICE)&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;HAVE _SMALL_1:&lt;/STRONG&gt; INCOME_LEVEL (NAME, INCOME, START_DATE, END_DATE)&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;HAVE _SMALL_2:&lt;/STRONG&gt;&amp;nbsp;STATUS (NAME, STATUS, START_DATE, END_DATE)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The two small tables may or may not have overlapping dates. In proc sql, I would do it like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;proc sql;
create table FINAL_TABLE as select
	a.name,
	a.record,
	a.date_of_service,
	b.income,
	c.status
from HAVE_BIG a
left join HAVE_SMALL_1 b
on 	a.name = b.name and
	a.date_of_service between b.start_date and b.end_date
left join HAVE_SMALL_2 c
on 	a.name = c.name and
	a.date_of_service between c.start_date and c.end_date
;quit;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In hash join, I'm doing it like below, but getting duplicate records and an incorrect output. Can anyone help me translate the proc sql statement above into a hash join, or edit the code below?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data FINAL_TABLE;
	if _n_ = 1 then do;
	    declare hash add1(dataset:'HAVE_SMALL_1',multidata: 'Y');
	    add1.defineKey('NAME');
	    add1.defineData('START_DATE','END_DATE','INCOME');
	    add1.defineDone();
	    declare hash add2(dataset:'HAVE_SMALL_2',multidata: 'Y');
	    add2.defineKey('NAME');
	    add2.defineData('START_DATE','END_DATE','STATUS');
	    add2.defineDone();
	end;

	set HAVE_BIG;

	format START_DATE date9. END_DATE date9. INCOME 10.5;
	if add1.find() = 0 then do until (add1.find_next());
	    if START_DATE le DATE_OF_SERVICE le END_DATE then do;
	      found=1;
	      output;
	    end;
	end;
	call missing(of INCOME);  *full list of values to clear - all of hash data elements;
	if not (found) then output;
	drop found START_DATE END_DATE;

	format START_DATE date9. END_DATE date9. STATUS 10.5;
	if add2.find() = 0 then do until (add2.find_next());
	    if START_DATE le DATE_OF_SERVICE le END_DATE then do;
	      found=1;
	      output;
	    end;
	end;
	call missing(of STATUS);  *full list of values to clear - all of hash data elements;
	if not (found) then output;
	drop found START_DATE END_DATE;
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!!!&lt;/P&gt;</description>
    <pubDate>Wed, 17 Feb 2021 19:57:12 GMT</pubDate>
    <dc:creator>derekcra</dc:creator>
    <dc:date>2021-02-17T19:57:12Z</dc:date>
    <item>
      <title>Multiple Multidata hash joins</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiple-Multidata-hash-joins/m-p/720016#M223005</link>
      <description>&lt;P&gt;Hello all -&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm looking for some help on how to do multiple multidata hash joins in tandem. Essentially, I'm trying to do two proc sql left joins in a hash join instead.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here are the tables I'm dealing with:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;HAVE_BIG:&lt;/STRONG&gt; RECORDS (NAME, RECORD_ID, DATE_OF_SERVICE)&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;HAVE _SMALL_1:&lt;/STRONG&gt; INCOME_LEVEL (NAME, INCOME, START_DATE, END_DATE)&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;HAVE _SMALL_2:&lt;/STRONG&gt;&amp;nbsp;STATUS (NAME, STATUS, START_DATE, END_DATE)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The two small tables may or may not have overlapping dates. In proc sql, I would do it like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;proc sql;
create table FINAL_TABLE as select
	a.name,
	a.record,
	a.date_of_service,
	b.income,
	c.status
from HAVE_BIG a
left join HAVE_SMALL_1 b
on 	a.name = b.name and
	a.date_of_service between b.start_date and b.end_date
left join HAVE_SMALL_2 c
on 	a.name = c.name and
	a.date_of_service between c.start_date and c.end_date
;quit;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In hash join, I'm doing it like below, but getting duplicate records and an incorrect output. Can anyone help me translate the proc sql statement above into a hash join, or edit the code below?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data FINAL_TABLE;
	if _n_ = 1 then do;
	    declare hash add1(dataset:'HAVE_SMALL_1',multidata: 'Y');
	    add1.defineKey('NAME');
	    add1.defineData('START_DATE','END_DATE','INCOME');
	    add1.defineDone();
	    declare hash add2(dataset:'HAVE_SMALL_2',multidata: 'Y');
	    add2.defineKey('NAME');
	    add2.defineData('START_DATE','END_DATE','STATUS');
	    add2.defineDone();
	end;

	set HAVE_BIG;

	format START_DATE date9. END_DATE date9. INCOME 10.5;
	if add1.find() = 0 then do until (add1.find_next());
	    if START_DATE le DATE_OF_SERVICE le END_DATE then do;
	      found=1;
	      output;
	    end;
	end;
	call missing(of INCOME);  *full list of values to clear - all of hash data elements;
	if not (found) then output;
	drop found START_DATE END_DATE;

	format START_DATE date9. END_DATE date9. STATUS 10.5;
	if add2.find() = 0 then do until (add2.find_next());
	    if START_DATE le DATE_OF_SERVICE le END_DATE then do;
	      found=1;
	      output;
	    end;
	end;
	call missing(of STATUS);  *full list of values to clear - all of hash data elements;
	if not (found) then output;
	drop found START_DATE END_DATE;
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!!!&lt;/P&gt;</description>
      <pubDate>Wed, 17 Feb 2021 19:57:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiple-Multidata-hash-joins/m-p/720016#M223005</guid>
      <dc:creator>derekcra</dc:creator>
      <dc:date>2021-02-17T19:57:12Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple Multidata hash joins</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiple-Multidata-hash-joins/m-p/720089#M223034</link>
      <description>&lt;P&gt;Please provide (as a data step or as a sql query) some data we can use for testing.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Feb 2021 02:56:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiple-Multidata-hash-joins/m-p/720089#M223034</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-02-18T02:56:02Z</dc:date>
    </item>
  </channel>
</rss>

