Latonya McHale
Latonya McHale

Reputation: 11

Converting type from character to numerical in SAS

First I had to format all of the categories to numbers and I used the code below to do that and was successful. I need to convert the type from character to numerical so that I can run analysis. I have tried the input function but that has not worked either. Any help would be greatly appreciated.

Proc Format;

Value $gender_num 'Male'=0 'Female'=1;
Value $att 'Yes'=0 'No'=1;
Value $bustrav 'Non-Travel'=1 'Travel_Frequently'=2 'Travel_Rarely'=3;
Value $dpt 'Research & Development'=1 'Human Resources'=2 'Sales'=3;
Value $edfd 'Life Sciences'=1 'Human Resources'=2 'Marketing'=3 'Medical'=4 'Technical Degree'=5 'Other'=6;
Value $ot 'Yes'=0 'No'=1;
Value $ms 'Divorced'=1 'Married'=2 'Single'=3;
Value $jr 'Healthcare Representative'=1 'Human Resources'=2 'Laboratory Technician'=3 'Manager'=4 

'Manufacturing Director'=5 'Research Director'=6 'Research Scientist'=7 'Sales Executive'=8 'Sales Representative'=9;

Run; Proc Print data=work.empatt;

format gender $gender_num.;
format attrition $att.;
format businesstravel $bustrav.;
format department $dpt.;
format educationfield $edfd.;
format overtime $ot.;
format maritalstatus $ms.;
format jobrole $jr.;

Run;

Upvotes: 1

Views: 489

Answers (1)

Joe
Joe

Reputation: 63434

You're mixing Formats with Informats.

  • Format: "How do I display a number on the screen for you?"
  • Informat: "How do I convert text to a number?"

Your code in the first step above should be invalues. Then you use input to translate. You also need to assign that to a new variable - you can't just associate the informat with the variable and magically get a numeric.

proc format;
  invalue sexi
     'Male'=1
     'Female'=2
  ;
quit;

data want;
  set have;
  sex_n = input(sex,sexi.);
run;

You can, if you want, keep the same variable name; I'll show that in the next step, also adding a format so the value "looks" right.

proc format;
  invalue sexi
     'Male'=1
     'Female'=2
  ;
  value sexf
    1 = 'Male'
    2 = 'Female'
  ;
quit;

data want;
  set have;
  sex_n = input(sex,sexi.);
  format sex_n sexf.;
  drop sex;
  rename sex_n = sex;
run;

You drop the original one, then rename the new one to the original name. I use the _n suffix to make it clear what I'm doing, but it's not required; nor are the 'i' and 'f' suffixes in the format/informat (and in fact you could use the identical name if you wanted to), again just a pattern I use to make it easier to distinguish.

Upvotes: 2

Related Questions