Hash object understanding help

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 75
Accepted Solution

Hash object understanding help

I have the below hash code:

data in;
input B_ID $ Client_name & $20. Client_Id $;
datalines;
2 carlsberg A146
7 kronenbourg b123
5 stella artois c987
23 tuborg g143
45 budweiser h765
73 Miller b876
5 heineken c986
5 Fosters l127
;
data in2;
input (BV_id Var1 Var2 Var3 Var4 Var5) ($);
datalines;
5 x y z 23 25
2 a b c 45 24
7 j k l 34 65
5 x y z 21 24
72 x y z 20 22
;

data results;
if 0 then set in;
if _N_ = 1 then do;
declare hash h(dataset:'in');
h.defineKey('b_id');
h.defineData('b_id','client_name', 'client_id');
h.defineDone();
end;
set in2;
if h.find(key:BV_id) = 0 then
output;
run;

 

I understand the find method writes results only if a match is found from the dataset in2 as it looks into the hash object 'in' for a match.

1.Is my understanding correct? 

2. If you notice the last record in in2 dataset: 72 x y z 20 22  , I would like to tweak the above code to include all those records in in2 dataset that does not have a match in hash object like that of a left join. Can anybody help me with the tweak? Thanks!

 


Accepted Solutions
Solution
‎10-19-2015 01:18 PM
Respected Advisor
Posts: 4,137

Re: Hash object understanding help

[ Edited ]

Just "output" every record and not only the ones with a match. 

data results;
  if _N_ = 1 then
    do;
      if 0 then set in;
      declare hash h(dataset:'in');
      h.defineKey('b_id');
      h.defineData('b_id','client_name', 'client_id');
      h.defineDone();
    end;
  set in2;
  if h.find(key:BV_id) ne 0 then call missing(b_id ,client_name, client_id);
run;

Compared to a Left Join the hash lookup as implemented will only return the same result if the hash key "b_id" is unique (=only 1 record in the hash table with the same key value).

 

The "h.find()" method used in an IF condition still will do the lookup. The function itself will return "0" if there had been a match.

We need the "call missing()" if there hasn't been a match to not "drag on" values from the last successful lookup.

View solution in original post


All Replies
Super Contributor
Posts: 290

Re: Hash object understanding help

After SET IN2;

add these:

rc = h.find(key:BV_id);
if rc ^= 0 then call missing(b_id, client_name, client_id);
drop rc;
Solution
‎10-19-2015 01:18 PM
Respected Advisor
Posts: 4,137

Re: Hash object understanding help

[ Edited ]

Just "output" every record and not only the ones with a match. 

data results;
  if _N_ = 1 then
    do;
      if 0 then set in;
      declare hash h(dataset:'in');
      h.defineKey('b_id');
      h.defineData('b_id','client_name', 'client_id');
      h.defineDone();
    end;
  set in2;
  if h.find(key:BV_id) ne 0 then call missing(b_id ,client_name, client_id);
run;

Compared to a Left Join the hash lookup as implemented will only return the same result if the hash key "b_id" is unique (=only 1 record in the hash table with the same key value).

 

The "h.find()" method used in an IF condition still will do the lookup. The function itself will return "0" if there had been a match.

We need the "call missing()" if there hasn't been a match to not "drag on" values from the last successful lookup.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 240 views
  • 0 likes
  • 3 in conversation