DATA Step, Macro, Functions and more

HASH mistype error

Reply
Contributor
Posts: 24

HASH mistype error


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.

PROC Star
Posts: 7,363

Re: HASH mistype 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)?

Contributor
Posts: 24

Re: HASH mistype error

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.

SAS Super FREQ
Posts: 683

Re: HASH mistype 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:

data student_dim;
  do student_skey = 1 to 10;
    student_id = put(student_skey,
roman.);
    output;
 
end;
run;

data test;
  length
    student_skey
8
    student_id $
8
  ;

 
call missing(student_skey, student_id);
  if _n_=1 then do;
   
declare hash e(dataset: 'student_dim',  ordered: 'yes');
    e.definekey('student_skey');
    e.definedata('student_skey');
    e.definedata('student_id');
    e.definedone();
   
declare hiter ei('e');
  end;


 
do while( ei.next() = 0);
    putlog _all_;
  end;
run;
Contributor
Posts: 24

Re: HASH mistype error

Bruno, I am reading the dataset from our data warehouse. Does the definedata and definekey have to be character?

Respected Advisor
Posts: 3,777

Re: HASH mistype error

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.

Modifying example.

data student_dim;
   do student_skey = 1 to 10;
      student_id = put(student_skey,
roman.);
      output;
     
end;
  
run;

data test;
   if 0 then set student_dim; 
  
if _n_=1 then do;
     
declare hash e(dataset: 'student_dim',  ordered: 'yes');
      e.definekey('student_skey');
      e.definedata('student_skey');
      e.definedata('student_id');
      e.definedone();
     
declare hiter ei('e');
      end;

  
do while( ei.next() = 0);
      putlog _all_;
      end;
  
stop;
  
run;
Respected Advisor
Posts: 3,124

Re: HASH mistype error

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

Ask a Question
Discussion stats
  • 6 replies
  • 1506 views
  • 0 likes
  • 5 in conversation