Hash Join

Reply
Occasional Contributor
Posts: 5

Hash Join

[ Edited ]

Please help with the code for hash join , for the below code in PROC SQL 

 

Proc sql;

Create table test as

Select *

From dsn1 a inner join dsn2 b 

On a.key1=b.key2 or a.key1a=b.key2;

Quit;

Regular Contributor
Posts: 161

Re: Hash Join

Appears a thread exists for probably what you are looking for... 

https://communities.sas.com/t5/SAS-Procedures/HASH-join/m-p/112108#U112108

Kannan Deivasigamani
Regular Contributor
Posts: 161

Re: Hash Join

This paper provides example of Hash merging using 2 keys....  Refer to page 5. 

 

http://www2.sas.com/proceedings/forum2008/029-2008.pdf

 

Hope this helps...

Kannan Deivasigamani
Respected Advisor
Posts: 4,138

Re: Hash Join

Providing sample data would help.

 

Below code sample assumes that the key in "dsn2" is unique. If this is not the case then you would need tag "multidata" in the hash and you would need to loop over the hash entries with the same key if there is a match.

data dsn1;
  input key1 key1a @@;
  source1='dsn1';
  output;
  datalines;
1 1 1 2 9 2 9 9
;
run;

data dsn2;
  input key2 @@;
  source2='dsn2';
  output;
  datalines;
1 2 3
;
run;

data want(drop=_rc);
  set dsn1;

  if _n_=1 then
    do;
      if 0 then set dsn2;
      dcl hash h(dataset:'dsn2');
      _rc=h.defineKey('key2');
      _rc=h.defineData(all:'y');
      _rc=h.defineDone();
    end;

  if h.find(key:key1)=0 then
    do;
      match='key1 ';
      output;
    end;
  else if h.find(key:key1a)=0 then
    do;
      match='key1a';
      output;
    end;
run;
Ask a Question
Discussion stats
  • 3 replies
  • 367 views
  • 2 likes
  • 3 in conversation