09-29-2015 11:42 AM
I want to pass two or more macro variables as Key to a hash object.
%let KEY = Name Account_Number;
if _n_ = 1 then do;
if 0 then set Some_Data_Set;
declare hash h();
The following is the error.
ERROR: Undeclared key symbol name account_number for hash object at line 2158 column 7
09-29-2015 12:25 PM
In this case, it would be appropriate for you to do the first half of the work. Show us what the final program would look like if you were not using macro language, but just hard-coding the whole thing.
09-29-2015 05:07 PM
I believe the issue here is syntax. Try setting your macro variable like this:
%let KEY = Name", "Account_Number;
definekey and definedata are expecting each key and data variable to be surrounded by quotes and separated by a comma.
09-29-2015 06:11 PM
%let keys = 'Name', 'Account_Number'; data _null_; if _n_ = 1 then do; if 0 then set Some_Data_Set; declare hash h(); h.definekey(&keys.); h.definedata(&keys.); h.definedone(); end; run;
would be the clean way to pass those values imho.
09-29-2015 06:30 PM
Your solution works. Thank you.
Your solution works too. Thanks.
I found that the KEY has also to be used in KEEP= SET option. I wanted to use in "data out(keep = &KEY);" . Both your suggestions will
not directly work. I found it convenient to split the KEY into two:
%let key1 = name;
%let key2 = account_number;
then I used
09-30-2015 01:40 PM
I completely agree with ChrisNZ, much cleaner.
One additional point...you can use call missing for any variables that are defined as data in the hash if they do not exist anywhere else in the data step, to avoid the warning you get in the log for undefined variables.
09-30-2015 03:00 PM
Personally I have a utility macro that I use for things like this.
%let KEY = Name Account_Number; %put %qlist(&key); ('Name','Account_Number') h.definekey%qlist(&KEY);