Obsidian | Level 7

## How do convert categorical value to numeric ?

Dear Community ,

I have tried to convert the value from categorical data to numeric no success. In my SAS data set (raw data), I have values like 20,25, and 15,10,0. I tried to use the format BEST12. and 8. , it still did not work and I got:
Old_Var New_Var (8.)
25 2
20 2
15 1

Any help welcome

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Quartz | Level 8

## Re: How do convert categorical value to numeric ?

I doubt if this type of data is Categorical, this seems like its a numerical data, kindly provide more info. say a screenshot of what you have.
6 REPLIES 6
Quartz | Level 8

## Re: How do convert categorical value to numeric ?

I doubt if this type of data is Categorical, this seems like its a numerical data, kindly provide more info. say a screenshot of what you have.
Obsidian | Level 7

## Re: How do convert categorical value to numeric ?

Yes it's is . The original format is
1="25"
2="20"
3="15"
4="10"
5="5"
6="0"
I have just found an alternative to recode it if old var1=1 then new var =25......
Super User

## Re: How do convert categorical value to numeric ?

@ndamo wrote:

Dear Community ,

I have tried to convert the value from categorical data to numeric no success. In my SAS data set (raw data), I have values like 20,25, and 15,10,0. I tried to use the format BEST12. and 8. , it still did not work and I got:
Old_Var New_Var (8.)
25 2
20 2
15 1

Any help welcome

Thanks

A FORMAT is not going to be much help.  FORMATs convert value to text.

Perhaps you meant you tried an INFORMAT?  INFORMATS convert text to values.  But BEST is not an INFORMAT (if you do use it as an informat SAS will just assume you meant to use the normal numeric informat).

You have not really explained what you are trying to do.  If you want to convert a CHARACTER value to numeric then use the INPUT() function with a numeric informat.  I suspect that your original variable had leading spaces (or was already numeric forcing SAS to first convert it to a character string that the INPUT() function needs) and did not use a large enough width on the informat specification you used.  Note that the INPUT() function does not mind if you use a width on the informat that is large then the length of the string being read.  The maximum width you can use with the numeric informat is 32.

``````data want;
set have;
number = input(string,32.);
run;``````

If you character variable is longer than 32 bytes then only the first 32 are read.  You might want to use the LEFT() function to remove any leading spaces.

``````data want;
set have;
number = input(left(string),32.);
run;``````
Obsidian | Level 7

## Re: How do convert categorical value to numeric ?

@Tom ,

Hi Tom ,

I have tried this but it did not work. The format in raw data is 1="25"
2="20"
3="15"
4="10"
5="5"
6="0"
I have just found an alternative to recode it if old var1=1 then new var =25......

Thank you

Super User

## Re: How do convert categorical value to numeric ?

@ndamo wrote:

@Tom ,

Hi Tom ,

I have tried this but it did not work. The format in raw data is 1="25"
2="20"
3="15"
4="10"
5="5"
6="0"
I have just found an alternative to recode it if old var1=1 then new var =25......

Thank you

So you started with a number.  And you want to convert the formatted values back into a number.  Since formats generate text you will need an INPUT() function to convert the generated text back into a number.  If you don't know the name of the format the variable is using, but the format is already attached to the variable you can use the VVALUE() function to get the formatted value.

``````data want;
set have;
new=input(left(vvalue(old)),32.)
run;``````
Obsidian | Level 7

## Re: How do convert categorical value to numeric ?

Yes ,Thank you for such wonderful help!
Discussion stats
• 6 replies
• 2254 views
• 3 likes
• 3 in conversation