DATA Step, Macro, Functions and more

Hash Data Lookup Question

Reply
Frequent Contributor
Posts: 139

Hash Data Lookup Question

Dear SAS Forum,

I'm just starting out with the hash object. I was under the impression that if the find method cannot find a match then "nothing" is returned in the datavalues. However it looks like the last value in the PDV is returned if the find method cannot match to the key.

I thought that I could mimic a left join with the hash object. I could use call missing on grade before the find method?

Can someone shed some light?

Thanks



data grades;
input gname $ grade $;
datalines;
Alfred A
Alice B
Barbara A
Carol D
Henry C
James B
Jane B
Janet C
Jeffrey A
John A
Joyce D
Judy F
Louise A
Mary A
Philip C
Robert C
Ronald C
;
run;


proc sql;
create table class1 as
select a.*, b.grade
from SASHELP.CLASS a left join grades b on a.name=b.gname
order by a.name;
quit;

data class2;

if 0 then set grades(obs=1);

if _n_ eq 1 then do;

declare hash grades(dataset:"grades");
grades.definekey("gname");
grades.definedata("grade");
grades.definedone();

end;

set sashelp.class;

rc=grades.find(key: name);
run;

proc compare data=class1 compare=class2;
run;
Super Contributor
Super Contributor
Posts: 3,174

Re: Hash Data Lookup Question

Posted in reply to darrylovia
My experience using the feature, as documented, is that the PDV is not altered (behavior I would definitely want to occur) - you will want to test rc (in your code) to determine how to proceed. The interpretation of your observation "..then 'nothing' is returned..." is not totally clear to me.

Scott Barry
SBBWorks, Inc.

SAS(R) 9.2 Language Reference: Dictionary - FIND method:
http://support.sas.com/documentation/cdl/en/lrdict/61724/HTML/default/a002588683.htm
Frequent Contributor
Posts: 139

Re: Hash Data Lookup Question

Ran another experiment.

By adding a call missing before the find method, the hash merge gets the same results as a LEFT JOIN. See the code below. If the call missing is not present
the grade for "William" is the grade for "Ronald". Hmmm


data grades;
input gname $ grade $;
datalines;
Alfred A
Alice B
Barbara A
Carol D
Henry C
James B
Jane B
Janet C
Jeffrey A
John A
Joyce D
Judy F
Louise A
Mary A
Philip C
Robert C
Ronald C
;
run;


proc sql;
create table class1 as
select a.*, b.grade
from SASHELP.CLASS a left join grades b on a.name=b.gname
order by a.name;
quit;

data class2;

if 0 then set grades(obs=1);

if _n_ eq 1 then do;

declare hash grades(dataset:"grades");
grades.definekey("gname");
grades.definedata("grade");
grades.definedone();

end;

set sashelp.class;
rc=grades.find(key: name);
if rc ne 0 then call missing(grade);
run;

proc compare data=class1 compare=class2;
run;
Ask a Question
Discussion stats
  • 2 replies
  • 111 views
  • 0 likes
  • 2 in conversation