I am receiving this error for my hash portion of my code:
ERROR: Type mismatch for method parameter 1 at line 31 column 3.
ERROR: Expecting Character type.
ERROR: DATA STEP Component Object failure. Aborted during the EXECUTION phase.
This is my code:
if _n_=1 then do;
declare hash e(dataset:'myasiq.student_dim');
e.definekey(student_skey);
e.definedata('student_id');
e.definedone();
end;
My student_skey is a numeric field and student_id is character. I am not sure what is generating the error.
Two thoughts: (1) Does the code preceding the code you posted include length statements for student_skey and student_id? and (2) have you tried to put quotes around student_skey (i.e., 'student_skey)?
I am not using the length statements since it is coming from our database. I did try the quotes around student_skey, but that gave another error.
The defineData and defineKey methods need a char argument.
It is also very important to define the variables from the hash object also in the PDV.
See an example below:
Bruno, I am reading the dataset from our data warehouse. Does the definedata and definekey have to be character?
I think the un-executed SET statement may be useful here. That way you don't have to know what the attributes are and they are not hard coded into your program. You will need STOP statement however.
I think what gets you confused is the 'variable name' and 'variable type' in the Hash definition. The quoted notion such as 'student_skey' in e.definekey('student_skey'); has nothing to do with the 'variable type', it merely addresses the 'variable name', where the variable can be either Char or Num, defined by incoming table or PDV. See following example,
data _null_;
if _n_=1 then
do;
if 0 then
set sashelp.class;
declare hash h(dataset: 'sashelp.class');
/* h.definekey('age');*/
new_age='age';
h.definekey(new_age);
h.definedata('name');
h.definedone();
end;
rc=h.output(dataset:'help');
run;
So quoted or not merely saying whether it is a 'string' or a variable. If it is a 'string', then 'string' itself is the variable name, if not quoted (variable), the content of the variable is the variable name.
Haikuo
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 25. 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.