DATA Step, Macro, Functions and more

hash object

Reply
Frequent Contributor
Frequent Contributor
Posts: 133

hash object

 

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. 

 

 

 

Super Contributor
Posts: 255

Re: hash object

 

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.

 

Frequent Contributor
Frequent Contributor
Posts: 133

Re: hash object

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.

Super User
Posts: 9,682

Re: hash object

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;

Frequent Contributor
Posts: 127

Re: hash object

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
Ask a Question
Discussion stats
  • 4 replies
  • 338 views
  • 0 likes
  • 4 in conversation