BookmarkSubscribeRSS Feed
SASPhile
Quartz | Level 8

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

3 REPLIES 3
art297
Opal | Level 21

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

 

mkeintz
PROC Star

 

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.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
lakshmi_74
Quartz | Level 8
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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 8802 views
  • 3 likes
  • 4 in conversation