<?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 HASH Object - Multiple SQL LEFT JOIN in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/HASH-Object-Multiple-SQL-LEFT-JOIN/m-p/43006#M8811</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I need help to create a hash join equivalent to using two left joins , on total 3 tables like&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Proc sql;&lt;/P&gt;&lt;P&gt;create table test as select * from&lt;/P&gt;&lt;P&gt;Table1 as A&amp;nbsp; left join &lt;/P&gt;&lt;P&gt;Table2 as B on (A.id=B.id)&amp;nbsp;&amp;nbsp;&amp;nbsp; left join&lt;/P&gt;&lt;P&gt;Table3 as&amp;nbsp; on (A.key=C.Key)&lt;/P&gt;&lt;P&gt;; quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For one table the following would work &lt;/P&gt;&lt;P&gt;assuming Table&amp;nbsp; T1 &amp;gt;&amp;nbsp; Table T2&lt;/P&gt;&lt;P&gt;data SET1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if 0 then set Table2 ;&lt;/P&gt;&lt;P&gt;declare hash VS(hashexp:7, dataset:Table2 );&lt;/P&gt;&lt;P&gt;VS.definekey('KEY'');&lt;/P&gt;&lt;P&gt;VS.definedata(all:'Y');&lt;/P&gt;&lt;P&gt;VS.definedone();&lt;/P&gt;&lt;P&gt;do until(eof);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;set Table1 end=eof;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if VS.find()=0 then output;&lt;/P&gt;&lt;P&gt;else do;&lt;/P&gt;&lt;P&gt;call missing(of T1-T2);&lt;/P&gt;&lt;P&gt;output;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;stop; run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 8pt;"&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>Thu, 04 Aug 2011 15:47:53 GMT</pubDate>
    <dc:creator>tommy81</dc:creator>
    <dc:date>2011-08-04T15:47:53Z</dc:date>
    <item>
      <title>HASH Object - Multiple SQL LEFT JOIN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HASH-Object-Multiple-SQL-LEFT-JOIN/m-p/43006#M8811</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I need help to create a hash join equivalent to using two left joins , on total 3 tables like&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Proc sql;&lt;/P&gt;&lt;P&gt;create table test as select * from&lt;/P&gt;&lt;P&gt;Table1 as A&amp;nbsp; left join &lt;/P&gt;&lt;P&gt;Table2 as B on (A.id=B.id)&amp;nbsp;&amp;nbsp;&amp;nbsp; left join&lt;/P&gt;&lt;P&gt;Table3 as&amp;nbsp; on (A.key=C.Key)&lt;/P&gt;&lt;P&gt;; quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For one table the following would work &lt;/P&gt;&lt;P&gt;assuming Table&amp;nbsp; T1 &amp;gt;&amp;nbsp; Table T2&lt;/P&gt;&lt;P&gt;data SET1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if 0 then set Table2 ;&lt;/P&gt;&lt;P&gt;declare hash VS(hashexp:7, dataset:Table2 );&lt;/P&gt;&lt;P&gt;VS.definekey('KEY'');&lt;/P&gt;&lt;P&gt;VS.definedata(all:'Y');&lt;/P&gt;&lt;P&gt;VS.definedone();&lt;/P&gt;&lt;P&gt;do until(eof);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;set Table1 end=eof;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if VS.find()=0 then output;&lt;/P&gt;&lt;P&gt;else do;&lt;/P&gt;&lt;P&gt;call missing(of T1-T2);&lt;/P&gt;&lt;P&gt;output;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;stop; run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 8pt;"&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>Thu, 04 Aug 2011 15:47:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HASH-Object-Multiple-SQL-LEFT-JOIN/m-p/43006#M8811</guid>
      <dc:creator>tommy81</dc:creator>
      <dc:date>2011-08-04T15:47:53Z</dc:date>
    </item>
    <item>
      <title>HASH Object - Multiple SQL LEFT JOIN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HASH-Object-Multiple-SQL-LEFT-JOIN/m-p/43007#M8812</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You can have multiple hash objects in one data step.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 09 Aug 2011 16:39:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HASH-Object-Multiple-SQL-LEFT-JOIN/m-p/43007#M8812</guid>
      <dc:creator>Daryl</dc:creator>
      <dc:date>2011-08-09T16:39:34Z</dc:date>
    </item>
    <item>
      <title>Re: HASH Object - Multiple SQL LEFT JOIN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HASH-Object-Multiple-SQL-LEFT-JOIN/m-p/43008#M8813</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Unsure what the issue is. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This should do what you want and be faster than SQL, provided the tables fit in memory and have unique keys of course.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt; &lt;/PRE&gt;&lt;P&gt;data SET1;&lt;BR /&gt;&amp;nbsp; set TABLE1 ;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;&amp;nbsp; if _N_=1 then do;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; declare hash VS( dataset:'TABLE2' );&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; VS.definekey('KEY');&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; VS.definedata(all:'Y');&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; VS.definedone();&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; declare hash VS1( dataset:'TABLE3' );&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; VS1.definekey('KEY');&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; VS1.definedata(all:'Y');&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; VS1.definedone();&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if 0 then set TABLE2 TABLE3;&lt;BR /&gt;&amp;nbsp; end;&lt;BR /&gt;&amp;nbsp; RC=VS.find();&lt;BR /&gt;&amp;nbsp; RC=VS1.find();&lt;BR /&gt;&amp;nbsp; drop RC;&lt;BR /&gt;proc print;run;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;To ensure the hash table variables are set to missing before retrieval, use the --&amp;nbsp; variable list shortcut.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data TABLE1;&lt;BR /&gt;&amp;nbsp; array B {5};&lt;BR /&gt;&amp;nbsp; KEY=1; B1=2;&amp;nbsp; output;&lt;BR /&gt;&amp;nbsp; KEY=2; B1=.;&amp;nbsp; output;&lt;/P&gt;&lt;P&gt;data TABLE2;&lt;BR /&gt;&amp;nbsp; array C {5};&lt;BR /&gt;&amp;nbsp; KEY=1; C1=2;&amp;nbsp; output;&lt;BR /&gt;&amp;nbsp; KEY=2; C1=.;&amp;nbsp; output;&lt;/P&gt;&lt;P&gt;data TABLE3;&lt;BR /&gt;&amp;nbsp; array D {5};&lt;BR /&gt;&amp;nbsp; KEY=1; D1=2;&amp;nbsp; output;&lt;BR /&gt;&amp;nbsp; KEY=2; D1=4;&amp;nbsp; output;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data SET1;&lt;BR /&gt;&amp;nbsp; set TABLE1 ;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;&amp;nbsp; if _N_=1 then do;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; declare hash VS( dataset:'TABLE2' );&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; VS.definekey('KEY');&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; VS.definedata(all:'Y');&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; VS.definedone();&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; declare hash VS1( dataset:'TABLE3' );&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; VS1.definekey('KEY');&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; VS1.definedata(all:'Y');&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; VS1.definedone();&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if 0 then set TABLE2 TABLE3;&lt;BR /&gt;&amp;nbsp; end;&lt;BR /&gt;&amp;nbsp; call missing(of C1--D5);&lt;BR /&gt;&amp;nbsp; RC=VS.find();&lt;BR /&gt;&amp;nbsp; RC=VS1.find();&lt;BR /&gt;&amp;nbsp; drop RC;&lt;BR /&gt;proc print;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 10 Aug 2011 05:39:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HASH-Object-Multiple-SQL-LEFT-JOIN/m-p/43008#M8813</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2011-08-10T05:39:28Z</dc:date>
    </item>
    <item>
      <title>Re: HASH Object - Multiple SQL LEFT JOIN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HASH-Object-Multiple-SQL-LEFT-JOIN/m-p/43009#M8814</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am very interesting about your questio.&lt;/P&gt;&lt;P&gt;But since you did not post some sample data,so it is hard to give a exact code. This is an example.&lt;/P&gt;&lt;P&gt;Whether it is what you want.&lt;/P&gt;&lt;P&gt;Maybe in the future, I will use Hash Table to make left join and Product Cartesian . Thank you . &lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.sas.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&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;PRE&gt;data class1(keep=name sex age) ;
 set sashelp.class;
run;
data class2(keep=sex weight rename=(sex=_sex));
 set sashelp.class;
 if ranuni(-1) le .8;
run;
data class3(keep=name height rename=(name=_name));
 set sashelp.class;
 if ranuni(-1) ge .8;
run;
data _want(drop=a1 _: rc found);
 set class1;
if _n_ eq 1 then do;
 declare hash ha1(hashexp:10);
 declare hiter _ha1('ha1');
&amp;nbsp; ha1.definekey('_sex','a1');
&amp;nbsp; ha1.definedata('weight','_sex');
&amp;nbsp; ha1.definedone();

do until(last);
 set class2 end=last;
 a1+1;
 ha1.add();
end;

end;



rc=_ha1.first();
do while(not rc);
rc=_ha1.next();
if sex=_sex then do;output; found=1;call missing(a1,_sex,weight);end;
end;
if not found then do;call missing(a1,_sex,weight);output;end;

run; 
data want(drop= a2 _: rc found);
 set _want;
if _n_ eq 1 then do;

 declare hash ha2(hashexp:10);
 declare hiter _ha2('ha2');
&amp;nbsp; ha2.definekey('_name','a2');
&amp;nbsp; ha2.definedata('height','_name');
&amp;nbsp; ha2.definedone();


do until(_last);
 set class3 end=_last;
 a2+1;
 ha2.add();
end;

end;


rc=_ha2.first();
do while(not rc);
rc=_ha2.next();
if name=_name then do;output;found=1;call missing(a2,_name,height);end;
end;
if not found then do;call missing(a2,_name,height);output;end;

run; 

&lt;/PRE&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;&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;Ksharp&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 11 Aug 2011 09:54:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HASH-Object-Multiple-SQL-LEFT-JOIN/m-p/43009#M8814</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-08-11T09:54:32Z</dc:date>
    </item>
    <item>
      <title>Re: HASH Object - Multiple SQL LEFT JOIN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HASH-Object-Multiple-SQL-LEFT-JOIN/m-p/624934#M184157</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;is there a way you can do this multi left join using sas hash object, but without reading in datasets multiple times inorder to rename variables? My thought is that you have 3 datasets and they're all joined on a composite key of the same 2 variables in each dataset. How could I do this without renaming variables? Thanks&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Feb 2020 20:08:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HASH-Object-Multiple-SQL-LEFT-JOIN/m-p/624934#M184157</guid>
      <dc:creator>juan1mario</dc:creator>
      <dc:date>2020-02-14T20:08:08Z</dc:date>
    </item>
    <item>
      <title>Re: HASH Object - Multiple SQL LEFT JOIN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HASH-Object-Multiple-SQL-LEFT-JOIN/m-p/624986#M184177</link>
      <description>&lt;P&gt;This post is very old.&lt;/P&gt;
&lt;P&gt;Please start a new session ,let more people to see it.You could get better answer.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;"How could I do this without renaming variables?"&lt;/P&gt;
&lt;P&gt;I think could use this kind of statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;h.find(key:var1,key:var2)&lt;/P&gt;</description>
      <pubDate>Sat, 15 Feb 2020 03:39:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HASH-Object-Multiple-SQL-LEFT-JOIN/m-p/624986#M184177</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-02-15T03:39:00Z</dc:date>
    </item>
  </channel>
</rss>

