BookmarkSubscribeRSS Feed
CP2
Pyrite | Level 9 CP2
Pyrite | Level 9

 

I am using hash objects but when I have more than one key variable I get errors or unintended results. Can someone tell me what might be happening here:

 

This code is not working:

 

data want (drop=rc rc1 rc2 rc3);
length value 8 ;

/*create hash object once*/
if _n_=1 then do;
declare hash hmerge(dataset: 'NewData');
rc1 = hmerge.defineKey('ID', 'lot');
rc2 = hmerge.defineData('Value');
rc3 = hmerge.defineDone();
end;

set have ;
rc=hmerge.find(key:'ID', key: 'lot');
if rc=0 then value2 = value ;
else value2=. ;
run;

 

error message:

ERROR: Argument length greater than length of key variable ID at line 7695 column
8.
ERROR: Keys and data must be specified in the same order and have the same types as given in
DefineKey/DefineData at line 7695 column 8.
ERROR: DATA STEP Component Object failure. Aborted during the EXECUTION phase.

 

 

There are multiple variables between ID and lot but they are in ID, Lot order in both datasets. Also the type and length is the same. The only difference is informat.

 

Also i have used a "do until (EOF)" in some hash code as well but have the same problem when using two key variables. 

 

 

 

4 REPLIES 4
KachiM
Rhodochrosite | Level 12

 

vrc=hmerge.find(key:'ID', key: 'lot');

 

Because ID and lot comes from the data set(HAVE), take off the quotes for both the variables. Actually, simply use FIND() without giving the KEYs lile hmerge.find(); The reason is that both the variables are already in the Pgrogram Data Vector from your HAVE data set.

 

CP2
Pyrite | Level 9 CP2
Pyrite | Level 9

Thank you. I was using find() without the variable names but if one of the keys was not found in the hash object I was still getting a value so the 'if rc=0 ' criteria was coming out true when it should not have. I thought entering the specific variables was necessary but it might be because of a different problem. I'm still investigating this but if you happen to know of reasons why this could be happening I'd love any ideas. Thanks.

Ksharp
Super User

You need initialize variables in PDV before declare a hash object.

 

data want (drop=rc rc1 rc2 rc3);
length value 8 ;

/*create hash object once*/
if _n_=1 then do;

if 0 then set NewData; /*<------------*/
declare hash hmerge(dataset: 'NewData');
rc1 = hmerge.defineKey('ID', 'lot');
rc2 = hmerge.defineData('Value');
rc3 = hmerge.defineDone();
end;

set have ;
rc=hmerge.find();/*<-------- no need specify if HAVE contain the same variable name ID,LOT*/
if rc=0 then value2 = value ;
else value2=. ;
run;

SuryaKiran
Meteorite | Level 14

Before using the Hash table the data fields and their properties must be defined. You can use length and format statements, however the more efficient way is using SET statement.

 

data want (drop=rc rc1 rc2 rc3);

If 1=2 then set NewData;

/*create hash object once*/
if _n_=1 then do;
declare hash hmerge(dataset: 'NewData');
rc1 = hmerge.defineKey('ID', 'lot');
rc2 = hmerge.defineData('Value');
rc3 = hmerge.defineDone();
end;

set have ;
rc=hmerge.find(key:'ID', key: 'lot');
if rc=0 then value2 = value ;
else value2=. ;
run;

 

The SET statement will be read at compilation, data variable properties in the datasets will be extracted, but the statement will never actually execute due to the obviously false statement "if 1=2" condition.

 

Thanks,
Suryakiran

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
  • 4 replies
  • 3002 views
  • 0 likes
  • 4 in conversation