DATA Step, Macro, Functions and more

Using hash tables

Reply
Contributor
Posts: 42

Using hash tables

I have a dataset created 'abc' in a sasgrid location

I'm creating another dataset 'xyz' through that using hash table, for eg.,

 

data xyz;

if _n_=1 then do;

dcl hash hh (dataset: 'abc');

hh.definekey ('phoneno');

hh.definedone();

end;

followed by a bunch of keep statements

run;

 

when I try and run this, I get the following 2 errors:

ERROR: Undeclared key symbol phoneno for hash object
ERROR: DATA STEP Component Object failure. Aborted during the EXECUTION phase.

 

what does this even mean? what do I need to change to sort this? sorry I'm very new to hash tables

Contributor
Posts: 42

Re: Using hash tables

Posted in reply to astha8882000

Instead of going that entire route, can I do a normal read from the dataset using

 

data xyz;

set abc;

keep ....

run;

 

but I was wondering why did whoever wrote the code (he left the company) add in the hash table anyway? what purpose was that serving?

Valued Guide
Posts: 591

Re: Using hash tables

Posted in reply to astha8882000

If you don't have the phoneno variable in your 'abc' dataset then you will get this type of error. Make sure that variable is present in the dataset.

Thanks,
Suryakiran
Contributor
Posts: 42

Re: Using hash tables

Posted in reply to SuryaKiran
I just checked, that variable is present in that dataset
PROC Star
Posts: 1,283

Re: Using hash tables

Posted in reply to astha8882000

Then it is because phoneno is not present in the PDV for the xyz data set. Add an appropriate length statement or an if 0 then set abc like this

 

data abc;
phoneno=123;
run;

data xyz;
if 0 then set abc;
if _n_=1 then do;
   dcl hash hh(dataset: 'abc');
   hh.definekey('phoneno');
   hh.definedone();
end;
run;

Or.. Simply remove the entire do group with the hash declaration if it does not serve a purpose.

PROC Star
Posts: 1,283

Re: Using hash tables

Posted in reply to astha8882000

Run a PROC CONTENTS step like this

 

proc contents data=abc;
run;

and verify that the variable phoneno is present in the data set abc. 

 

Also, if the DO Group is only followed by a bunch of Keep Statements and the hash object hh is not used, simply remove the entire do group. No reason for him to have left it there.

Contributor
Posts: 42

Re: Using hash tables

the only place that hh is used later is this condition:
%include inclf(f1.sas)
if hh.find()=0;
%include inclf(f2.sas)

what does this condition mean so I could replace this too and get rid of hh altogether?
Super User
Posts: 10,241

Re: Using hash tables

[ Edited ]
Posted in reply to astha8882000

@astha8882000 wrote:
the only place that hh is used later is this condition:
%include inclf(f1.sas)
if hh.find()=0;
%include inclf(f2.sas)

what does this condition mean so I could replace this too and get rid of hh altogether?

You need to have a variable phoneno present (from any source) in your datastep, so that the find() method has a reference.

The

ERROR: Undeclared key symbol phoneno for hash object

happens ONLY when the variable is not there, period. Run the codes I gave you in the other thread for verifying this.

 

PS to be more specific: the presence of phoneno in the dataset used to create the hash DOES NOT create a datastep variable phoneno. That has to come from another source, eg another dataset.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 10,241

Re: Using hash tables

Posted in reply to astha8882000

This is obviously a continuation of https://communities.sas.com/t5/Base-SAS-Programming/Hash-Tables/m-p/477201.

Should I merge the threads?

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 42

Re: Using hash tables

Posted in reply to KurtBremser
Sure, thank you! I wasn't sure how to do that.
PROC Star
Posts: 1,805

Re: Using hash tables

Posted in reply to astha8882000

@astha8882000 Good morning and very good question. This raises the challenges of how sas compiles a hash object before execution phase starts.

 

Even if you do have the phoneno variable in your dataset 'abc'. you would still get that error. That's because hash object dot notation aren't compiled like the traditional variables in PDV when read from a sas dataset.

 

Assuming you have phoneno variable, the simple fix is:

 

data xyz;

if _n_=1 then do;

if 0 then set abc; /*notice this statement forcing sas to compile variables in the hash obj*/

dcl hash hh (dataset: 'abc');

hh.definekey ('phoneno');

hh.definedone();

end;

run;

 

Try to run with and without the above statement in bold, and see the log. You will notice the difference

 

Contributor
Posts: 42

Re: Using hash tables

Posted in reply to novinosrin
Is there a way to get rid of hash tables altogether? The only place that it is used later in the code is this:

%include inclf(f1.sas)
if hh.find()=0;
%include inclf(f2.sas)

does this condition mean if there are no records in hh, which means if it read no records from dataset abc, only then the include f2 is run else not? If I could replace this too and get rid of hh altogether?

So for the initial part of the code I could just read the dataset abc into xyz using data and set steps like I would normally do, do you think that might work?
Super User
Posts: 10,241

Re: Using hash tables

Posted in reply to astha8882000
%include inclf(f1.sas)
if hh.find()=0;
%include inclf(f2.sas)

I guess this piece of code shoold do the following:

In the first include (or in the code preceding that) a variable phoneno is either created or read from an incoming dataset.

The subsetting if checks if that phoneno is present in your reference dataset abc, which has been loaded into the hash (a successful find returns 0)

The code from the second include is only executed if the condition is met (the lookup found the phoneno).

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
PROC Star
Posts: 1,805

Re: Using hash tables

Posted in reply to astha8882000

"does this condition mean if there are no records in hh, which means if it read no records from dataset abc, only then the include f2 is run else not? If I could replace this too and get rid of hh altogether?"

 

what's in your f1.sas and f2.sas? Does it depend on the vairables in hash tables?

Contributor
Posts: 42

Re: Using hash tables

[ Edited ]
Posted in reply to novinosrin

F1 code:

attrib;
[a list of variables that are neither is abc or xyz]
set Q3;
a bunch of if conditions;
keep _all_;

F2 code:


length a $1;
if b in (....) then a='y' else a='n';
where b is a variable present in the dataset abc but it's not phoneno

Ask a Question
Discussion stats
  • 31 replies
  • 137 views
  • 4 likes
  • 6 in conversation