BookmarkSubscribeRSS Feed
TashaBee
Fluorite | Level 6


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.

6 REPLIES 6
art297
Opal | Level 21

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

TashaBee
Fluorite | Level 6

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.

BrunoMueller
SAS Super FREQ

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;
TashaBee
Fluorite | Level 6

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

data_null__
Jade | Level 19

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;
Haikuo
Onyx | Level 15

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 8247 views
  • 1 like
  • 5 in conversation