@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.
This is obviously a continuation of https://communities.sas.com/t5/Base-SAS-Programming/Hash-Tables/m-p/477201.
Should I merge the threads?
@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
%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).
"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?
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
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;
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
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
@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!
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.
@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.
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: