BookmarkSubscribeRSS Feed
Kurt_Bremser
Super User

@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.

astha8882000
Obsidian | Level 7
Sure, thank you! I wasn't sure how to do that.
novinosrin
Tourmaline | Level 20

@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

 

astha8882000
Obsidian | Level 7
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?
Kurt_Bremser
Super User
%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).

novinosrin
Tourmaline | Level 20

"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?

astha8882000
Obsidian | Level 7

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

novinosrin
Tourmaline | Level 20

basically, if your hash obj doesn;t have any records and obj.find method wouldn't find the matching key in the hash object. This will result in a non zero return code. For example:

data xyz;

if _n_=1 then do;
if 0 then set sashelp.class;

dcl hash hh (dataset: 'sashelp.class(obs=0)');/*hash object with zero records*/

hh.definekey ('name');

hh.definedone();

end;
set sashelp.class;
rc=hh.find();/*return code non zero*/
run;
astha8882000
Obsidian | Level 7

So this is the structure of the overall code:

data xyz;
if _n_=1 then do;
dcl hash hh (dataset: 'abc');
hh.definekey ('phoneno');
hh.definedone();
end;
bunch of keep statements that are not part of abc or xyz
%INCLUDE INCLF(f1.sas);
if hh.find()=0;
%INCLUDE INCLF(f2.sas);
Bunch of if statements
run;

I think right now my main aim is to get rid of hash altogether since understanding is a little hard for me based on my lack of experience

novinosrin
Tourmaline | Level 20

Ok,  Can you create a new thread with a sample data and your required output so that the community can give you an alternative solution other than hashes? We don't even have to worry about hashes at all. You can get a completely new and simple script. Please make sure you get the requirements correct

novinosrin
Tourmaline | Level 20

@astha8882000 just noticed. But I can't see your expected output sample for your input data sample, meaning you what to accomplish like data have(i see there is this bit) , please provide data want(for your sample). Thank you!

astha8882000
Obsidian | Level 7

The problem with that is, because of all the errors with my hash table code, I'm unable to run it and see what the solution might look like. Hence I wanted to get the hash table replaced so I can check and see what the result might be.

Kurt_Bremser
Super User

@astha8882000 wrote:

The problem with that is, because of all the errors with my hash table code, I'm unable to run it and see what the solution might look like. Hence I wanted to get the hash table replaced so I can check and see what the result might be.


Which means it is time to go back to square one.

  • what data do you have?
  • what do you want to achieve?

Once these questions are answered, you can start designing your solution.

Code that you do not understand is per definition crap and has to be discarded for unmaintainability. It might be that it's not crap for someone else (eg the original creator), but since it's obviously not sufficiently documented for you, can it.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 31 replies
  • 4302 views
  • 4 likes
  • 6 in conversation