DATA Step, Macro, Functions and more

SAS hash tables, joining on keys with different names

Reply
Super Contributor
Posts: 673

SAS hash tables, joining on keys with different names

[ Edited ]

Unlike in proc sql, we can join the key variables though they donnt have the common names,can this be done in hash tables?

if  dataset A has key variables memberid and membername and

dataset B has key variables member_id and member_name.

in proc sql we can join on memberid=member_id and membername=memebr_name 

i dont know if we have to rename before doing hash merging

PROC Star
Posts: 7,492

Re: SAS hash tables, joining on keys with different names

Since one can rename when set(ting) a file, wouldn't that suffice? As for multiple keys, the hash object can take composite keys.

 

I would think including a rename option when setting one of the files shouldn't be a concern or am I missing something?

 

Art, CEO, AnalystFinder.com

 

Trusted Advisor
Posts: 1,022

Re: SAS hash tables, joining on keys with different names

 

The answer is yes.

 

Art has already mentioned that own could use a rename parameter.  But even that isn't required.

 

Consder.  If data sets A and B had the same name, you could do this:

 

data want;
  if _n_=1 then do;
    if 0 then set B ;
    declare hash bdata (dataset:'B');
      bdata.definekey('memberid','membername');
      bdata.definedata(all:'Y');
      bdata.definedone();
  end;
  set a;
  rc=b.find();
  if rc=0;
run;

 

 

But let's say dataset B has varnames   memid and memname.  You could, if you wanted, avoid renaming as follows:

 

data want;
  if _n_=1 then do;
    if 0 then set B ;
    declare hash bdata (dataset:'B');
      bdata.definekey('memid','memname');
      bdata.definedata(all:'Y');
      bdata.definedone();
  end;
  set a;
  rc=b.find(key:memberid,key:membername);
  if rc=0;
run;

 

In other words, when using the find method for hash table b, don't let it assume it should use memid and memname (which don't exist in data set A).  Instead just tell it to use the memberid as the value for first key, and membername as the second.  So memberid from A will be matched against memid from B, etc.

Contributor
Posts: 57

Re: SAS hash tables, joining on keys with different names

Absolutely You can do. Here is the code:
data result;
if 0 then set A;
declare hash adata(dataset:'A');
adata.definekey("memberid","membername");
adata.definedata(all:'yes');
adata.definedone();
do until (eof);
set B end=eof;
rc=adata.find(key:member_id,key:member_name);
if rc eq 0 then output;
end;
drop rc member_id member_name;
run;
Ask a Question
Discussion stats
  • 3 replies
  • 583 views
  • 3 likes
  • 4 in conversation