1
votes

I need to convert categorical data in variable REG into numeric and I am doing it as given below:

proc format;
  value $Reg
    "To"=1
    "Al"=2
    "No"=3
    "Cu"=4
    other=5;
run;

data new;set old;
  format REG $Reg.;
run;

I tried the method in SAS proc format statement and the column REG is updating with 5 in all values (in all cases). May I know how to convert it properly? Thanks for the help.

2
What are the values of REG in the dataset OLD? Print them with $QUOTE. format to see if they have leading spaces. - Tom
By mistake the values in OLD were not just same as the passed strings.. It is working now. - DOT

2 Answers

1
votes

You use a custom INFORMAT and INPUT statement to convert text to numbers.

Example:

proc format;
  invalue Reg
    "To"=1
    "Al"=2
    "No"=3
    "Cu"=4
    other=5;

data have;
  input reg $ measure @@;
  datalines;
To 1.25 Moo 2.5 Al 3.2 Ti 5.6
Po 6.78 H 3.67 He 9.1 No 4.44
Cu 8.12 Cu 8.21 Yt 1.5
;

data want;
  set have;
  reg_num = input (reg, Reg.);
run;

enter image description here

1
votes

Works fine?

proc format;
  value $Reg
    "To"=1
    "Al"=2
    "No"=3
    "Cu"=4
    other=5;
run;

data old;
input REG $;
datalines;
To
A1
No
Cu
;

data new;
  set old;
  format REG $Reg.;
run;

Be aware that the values in the Value Statement are case sensitive though. That could be why.