BookmarkSubscribeRSS Feed
darrylovia
Quartz | Level 8
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;
2 REPLIES 2
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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
darrylovia
Quartz | Level 8
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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 593 views
  • 0 likes
  • 2 in conversation