<?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: Merge/Hash/SQL Join in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Merge-Hash-SQL-Join/m-p/72419#M20992</link>
    <description>Thank you.&lt;BR /&gt;
&lt;BR /&gt;
I switched the datasets, and had to tweak a few other things, this one works.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data hash3 (drop=rc);&lt;BR /&gt;
  length postl_cd $5;&lt;BR /&gt;
  declare AssociativeArray hh () ;&lt;BR /&gt;
       rc = hh.DefineKey ( 'addr_key' ) ;&lt;BR /&gt;
       rc = hh.DefineData ( 'addr_key','postl_cd') ;&lt;BR /&gt;
       rc = hh.DefineDone () ;&lt;BR /&gt;
       do until ( eof1 ) ;&lt;BR /&gt;
        set provzip end = eof1 ;&lt;BR /&gt;
        rc = hh.add () ;&lt;BR /&gt;
       end ;&lt;BR /&gt;
       do until ( eof2 ) ;&lt;BR /&gt;
        set address end = eof2 ;&lt;BR /&gt;
        rc = hh.find () ;&lt;BR /&gt;
        if rc ^=0 then postl_cd='     ';&lt;BR /&gt;
        output;&lt;BR /&gt;
       end ;&lt;BR /&gt;
run ;&lt;BR /&gt;
&lt;BR /&gt;
proc print data=hash3;&lt;BR /&gt;
  title 'hash3';&lt;BR /&gt;
run;</description>
    <pubDate>Tue, 30 Mar 2010 18:03:50 GMT</pubDate>
    <dc:creator>MZunnurain</dc:creator>
    <dc:date>2010-03-30T18:03:50Z</dc:date>
    <item>
      <title>Merge/Hash/SQL Join</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Merge-Hash-SQL-Join/m-p/72416#M20989</link>
      <description>Hello,&lt;BR /&gt;
&lt;BR /&gt;
I'm trying to learn some hash object....The original was written in a merge, I tried proc sql and it is acceptable using left join, just wondering how to get the left join using hash object..&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data address;&lt;BR /&gt;
  addr_key=10000001;&lt;BR /&gt;
  address='test address1';&lt;BR /&gt;
  output;&lt;BR /&gt;
  addr_key=10000002;&lt;BR /&gt;
  address='test address2';&lt;BR /&gt;
  output;&lt;BR /&gt;
  addr_key=10000003;&lt;BR /&gt;
  address='test address3';&lt;BR /&gt;
  output;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc print u;&lt;BR /&gt;
  title 'address';&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data provzip;&lt;BR /&gt;
  addr_key=10000001;&lt;BR /&gt;
  postl_cd='87048';&lt;BR /&gt;
  output;&lt;BR /&gt;
  addr_key=10000002;&lt;BR /&gt;
  postl_cd='39735';&lt;BR /&gt;
  output;&lt;BR /&gt;
 run;&lt;BR /&gt;
 &lt;BR /&gt;
 proc print u;&lt;BR /&gt;
   title 'provzip';&lt;BR /&gt;
 run;&lt;BR /&gt;
  &lt;BR /&gt;
  &lt;BR /&gt;
data hash1 (drop=rc);&lt;BR /&gt;
  length postl_cd $5;&lt;BR /&gt;
  declare AssociativeArray hh () ;&lt;BR /&gt;
       rc = hh.DefineKey ( 'addr_key' ) ;&lt;BR /&gt;
       rc = hh.DefineData ( 'addr_key', 'address' ) ;&lt;BR /&gt;
       rc = hh.DefineDone () ;&lt;BR /&gt;
       call missing(postl_cd);&lt;BR /&gt;
       do until ( eof1 ) ;&lt;BR /&gt;
        set address end = eof1 ;&lt;BR /&gt;
        rc = hh.add () ;&lt;BR /&gt;
       end ;&lt;BR /&gt;
       do until ( eof2 ) ;&lt;BR /&gt;
        set provzip end = eof2 ;&lt;BR /&gt;
        rc = hh.find () ;&lt;BR /&gt;
        if rc ^=0 then postl_cd='     ';&lt;BR /&gt;
        if rc =0 then output;&lt;BR /&gt;
       end ;&lt;BR /&gt;
run ;&lt;BR /&gt;
&lt;BR /&gt;
proc print data=hash1;&lt;BR /&gt;
  title 'hash1';&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data hash2; &lt;BR /&gt;
  if _n_ = 1 then do; &lt;BR /&gt;
    if 0 then set address; &lt;BR /&gt;
    declare hash lookup(dataset:'address'); &lt;BR /&gt;
    lookup.defineKey('addr_key'); &lt;BR /&gt;
    lookup.definedata('address');&lt;BR /&gt;
    lookup.defineDone(); &lt;BR /&gt;
  end; &lt;BR /&gt;
&lt;BR /&gt;
  set provzip; &lt;BR /&gt;
  if lookup.find(key:addr_key) ne 0 then postl_cd='     ';&lt;BR /&gt;
run; &lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
PROC PRINT;&lt;BR /&gt;
  title 'hash2';&lt;BR /&gt;
RUN;&lt;BR /&gt;
&lt;BR /&gt;
proc sql;&lt;BR /&gt;
  create table sql1 as&lt;BR /&gt;
  select *&lt;BR /&gt;
  from address a&lt;BR /&gt;
  left join provzip b&lt;BR /&gt;
  on a.addr_key=b.addr_key&lt;BR /&gt;
  ;&lt;BR /&gt;
 quit;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc print u;&lt;BR /&gt;
  title 'sql';&lt;BR /&gt;
run;</description>
      <pubDate>Tue, 30 Mar 2010 15:52:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Merge-Hash-SQL-Join/m-p/72416#M20989</guid>
      <dc:creator>MZunnurain</dc:creator>
      <dc:date>2010-03-30T15:52:12Z</dc:date>
    </item>
    <item>
      <title>Re: Merge/Hash/SQL Join</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Merge-Hash-SQL-Join/m-p/72417#M20990</link>
      <description>Have you searched the SAS support &lt;A href="http://support.sas.com/" target="_blank"&gt;http://support.sas.com/&lt;/A&gt;  website.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.&lt;BR /&gt;
&lt;BR /&gt;
Suggested Google advanced search argument, this topic/post:&lt;BR /&gt;
&lt;BR /&gt;
left join replace with hash object site:sas.com</description>
      <pubDate>Tue, 30 Mar 2010 17:33:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Merge-Hash-SQL-Join/m-p/72417#M20990</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2010-03-30T17:33:13Z</dc:date>
    </item>
    <item>
      <title>Re: Merge/Hash/SQL Join</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Merge-Hash-SQL-Join/m-p/72418#M20991</link>
      <description>You seem to have the basic idea.  To get what you want use the logic of the hash1 (or hash2) but put the provzip data set into the hash table and iterate over the address data set. &lt;BR /&gt;
&lt;BR /&gt;
The logic you're using only gets rows for which you have a postal code; doing it the other way will get all addresses, with postal code missing if the lookup fails. &lt;BR /&gt;
&lt;BR /&gt;
Use:&lt;BR /&gt;
&lt;BR /&gt;
&amp;gt;  if rc ^=0 then postl_cd='     ';&lt;BR /&gt;
&amp;gt;        if rc =0 then output;&lt;BR /&gt;
&lt;BR /&gt;
but take the "if rc = 0" off the second statement.&lt;BR /&gt;
&lt;BR /&gt;
Note:  it's usual with a hash lookup like this to put the small data set into the hash table.  This beats merge by not requiring the large data set to be sorted.  Proc SQL is a viable alternative; only a test will tell which is better.&lt;BR /&gt;
&lt;BR /&gt;
Cheers,&lt;BR /&gt;
&lt;BR /&gt;
Jonathan&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&amp;gt; Hello,&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; I'm trying to learn some hash object....The original&lt;BR /&gt;
&amp;gt; was written in a merge, I tried proc sql and it is&lt;BR /&gt;
&amp;gt; acceptable using left join, just wondering how to get&lt;BR /&gt;
&amp;gt; the left join using hash object..&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; data address;&lt;BR /&gt;
&amp;gt;   addr_key=10000001;&lt;BR /&gt;
&amp;gt; address='test address1';&lt;BR /&gt;
&amp;gt;   output;&lt;BR /&gt;
&amp;gt; addr_key=10000002;&lt;BR /&gt;
&amp;gt;   address='test address2';&lt;BR /&gt;
&amp;gt; output;&lt;BR /&gt;
&amp;gt;   addr_key=10000003;&lt;BR /&gt;
&amp;gt; address='test address3';&lt;BR /&gt;
&amp;gt;   output;&lt;BR /&gt;
&amp;gt; n;&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; proc print u;&lt;BR /&gt;
&amp;gt;   title 'address';&lt;BR /&gt;
&amp;gt; n;&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; data provzip;&lt;BR /&gt;
&amp;gt;   addr_key=10000001;&lt;BR /&gt;
&amp;gt; postl_cd='87048';&lt;BR /&gt;
&amp;gt;   output;&lt;BR /&gt;
&amp;gt; addr_key=10000002;&lt;BR /&gt;
&amp;gt;   postl_cd='39735';&lt;BR /&gt;
&amp;gt; output;&lt;BR /&gt;
&amp;gt;  run;&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt;  proc print u;&lt;BR /&gt;
&amp;gt;   title 'provzip';&lt;BR /&gt;
&amp;gt; un;&lt;BR /&gt;
&amp;gt;   &lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; data hash1 (drop=rc);&lt;BR /&gt;
&amp;gt;   length postl_cd $5;&lt;BR /&gt;
&amp;gt; declare AssociativeArray hh () ;&lt;BR /&gt;
&amp;gt;        rc = hh.DefineKey ( 'addr_key' ) ;&lt;BR /&gt;
&amp;gt; rc = hh.DefineData ( 'addr_key', 'address' ) ;&lt;BR /&gt;
&amp;gt;        rc = hh.DefineDone () ;&lt;BR /&gt;
&amp;gt; call missing(postl_cd);&lt;BR /&gt;
&amp;gt;        do until ( eof1 ) ;&lt;BR /&gt;
&amp;gt;  set address end = eof1 ;&lt;BR /&gt;
&amp;gt;        rc = hh.add () ;&lt;BR /&gt;
&amp;gt; end ;&lt;BR /&gt;
&amp;gt;        do until ( eof2 ) ;&lt;BR /&gt;
&amp;gt;  set provzip end = eof2 ;&lt;BR /&gt;
&amp;gt;        rc = hh.find () ;&lt;BR /&gt;
&amp;gt;  if rc ^=0 then postl_cd='     ';&lt;BR /&gt;
&amp;gt;        if rc =0 then output;&lt;BR /&gt;
&amp;gt; end ;&lt;BR /&gt;
&amp;gt; run ;&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; proc print data=hash1;&lt;BR /&gt;
&amp;gt;   title 'hash1';&lt;BR /&gt;
&amp;gt; n;&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; data hash2; &lt;BR /&gt;
&amp;gt;   if _n_ = 1 then do; &lt;BR /&gt;
&amp;gt;   if 0 then set address; &lt;BR /&gt;
&amp;gt;   declare hash lookup(dataset:'address'); &lt;BR /&gt;
&amp;gt;   lookup.defineKey('addr_key'); &lt;BR /&gt;
&amp;gt;   lookup.definedata('address');&lt;BR /&gt;
&amp;gt;   lookup.defineDone(); &lt;BR /&gt;
&amp;gt; end; &lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt;   set provzip; &lt;BR /&gt;
&amp;gt; if lookup.find(key:addr_key) ne 0 then postl_cd='&lt;BR /&gt;
&amp;gt;     ';&lt;BR /&gt;
&amp;gt;  &lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; PROC PRINT;&lt;BR /&gt;
&amp;gt;   title 'hash2';&lt;BR /&gt;
&amp;gt; N;&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; proc sql;&lt;BR /&gt;
&amp;gt;   create table sql1 as&lt;BR /&gt;
&amp;gt; select *&lt;BR /&gt;
&amp;gt;   from address a&lt;BR /&gt;
&amp;gt; left join provzip b&lt;BR /&gt;
&amp;gt;   on a.addr_key=b.addr_key&lt;BR /&gt;
&amp;gt; ;&lt;BR /&gt;
&amp;gt;  quit;&lt;BR /&gt;
&amp;gt; un;&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; proc print u;&lt;BR /&gt;
&amp;gt;   title 'sql';&lt;BR /&gt;
&amp;gt; n;</description>
      <pubDate>Tue, 30 Mar 2010 17:36:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Merge-Hash-SQL-Join/m-p/72418#M20991</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-03-30T17:36:27Z</dc:date>
    </item>
    <item>
      <title>Re: Merge/Hash/SQL Join</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Merge-Hash-SQL-Join/m-p/72419#M20992</link>
      <description>Thank you.&lt;BR /&gt;
&lt;BR /&gt;
I switched the datasets, and had to tweak a few other things, this one works.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data hash3 (drop=rc);&lt;BR /&gt;
  length postl_cd $5;&lt;BR /&gt;
  declare AssociativeArray hh () ;&lt;BR /&gt;
       rc = hh.DefineKey ( 'addr_key' ) ;&lt;BR /&gt;
       rc = hh.DefineData ( 'addr_key','postl_cd') ;&lt;BR /&gt;
       rc = hh.DefineDone () ;&lt;BR /&gt;
       do until ( eof1 ) ;&lt;BR /&gt;
        set provzip end = eof1 ;&lt;BR /&gt;
        rc = hh.add () ;&lt;BR /&gt;
       end ;&lt;BR /&gt;
       do until ( eof2 ) ;&lt;BR /&gt;
        set address end = eof2 ;&lt;BR /&gt;
        rc = hh.find () ;&lt;BR /&gt;
        if rc ^=0 then postl_cd='     ';&lt;BR /&gt;
        output;&lt;BR /&gt;
       end ;&lt;BR /&gt;
run ;&lt;BR /&gt;
&lt;BR /&gt;
proc print data=hash3;&lt;BR /&gt;
  title 'hash3';&lt;BR /&gt;
run;</description>
      <pubDate>Tue, 30 Mar 2010 18:03:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Merge-Hash-SQL-Join/m-p/72419#M20992</guid>
      <dc:creator>MZunnurain</dc:creator>
      <dc:date>2010-03-30T18:03:50Z</dc:date>
    </item>
    <item>
      <title>Re: Merge/Hash/SQL Join</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Merge-Hash-SQL-Join/m-p/72420#M20993</link>
      <description>Hi.&lt;BR /&gt;
&lt;BR /&gt;
Use the dataset option in the object construct, and provide the dataset name you whish to load into the hash.&lt;BR /&gt;
Then loose all the EOFs and until loops. You just need to read an observation from one dataset and match with the other stored in the hash.&lt;BR /&gt;
&lt;BR /&gt;
Be aware that until 9.2, the dataset loaded into the hash should have unique keys.&lt;BR /&gt;
&lt;BR /&gt;
Check the online doc here:&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/lrdict/62618/HTML/default/a002576871.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/lrdict/62618/HTML/default/a002576871.htm&lt;/A&gt;&lt;BR /&gt;
&lt;BR /&gt;
Cheers from Portugal.&lt;BR /&gt;
&lt;BR /&gt;
Daniel Santos @ &lt;A href="http://www.cgd.pt" target="_blank"&gt;www.cgd.pt&lt;/A&gt;</description>
      <pubDate>Wed, 31 Mar 2010 10:02:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Merge-Hash-SQL-Join/m-p/72420#M20993</guid>
      <dc:creator>DanielSantos</dc:creator>
      <dc:date>2010-03-31T10:02:13Z</dc:date>
    </item>
  </channel>
</rss>

