BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
ndamo
Obsidian | Level 7

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
himself
Quartz | Level 8
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.

View solution in original post

6 REPLIES 6
himself
Quartz | Level 8
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.
ndamo
Obsidian | Level 7
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......
Tom
Super User Tom
Super User

@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;
ndamo
Obsidian | Level 7

@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 

Tom
Super User Tom
Super User

@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;
ndamo
Obsidian | Level 7
Yes ,Thank you for such wonderful help!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 2254 views
  • 3 likes
  • 3 in conversation