BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
_Grove_
Calcite | Level 5

I am trying to convert a few character variables to numeric, perform a conversion and then for one variable convert back to character.
I am struggling with the code. I had been using this page for reference: https://blogs.sas.com/content/sgf/2015/05/01/converting-variable-types-do-i-use-put-or-input/

 

Attached is a picture of the variables I have, variables I want, their formats, and the calculation for the conversion factor.

 

Also, surely there is a way to do this without lengthy format and informat statements?

 

Thank you for any help!

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Char to number, put()

Number to char, input()

 

There are specific reasons why these should be used.  You supply the formats/informats.  Whilst @PaigeMiller's suggestion of adding 0 to a character string is sufficient, this uses something called implicit conversion, this can lead to bugs in your code which is very hard to debug.  Always use input() or put() to convert data from one type to another, not just for completeness sake but also to make code nice and clean and illegible, i.e. it shows that you are converting.

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

Convert character to numeric

 

num_var=char_var+0;

Convert numeric to character

 

char_var = cats(num_var);
--
Paige Miller
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Char to number, put()

Number to char, input()

 

There are specific reasons why these should be used.  You supply the formats/informats.  Whilst @PaigeMiller's suggestion of adding 0 to a character string is sufficient, this uses something called implicit conversion, this can lead to bugs in your code which is very hard to debug.  Always use input() or put() to convert data from one type to another, not just for completeness sake but also to make code nice and clean and illegible, i.e. it shows that you are converting.

Astounding
PROC Star

Who is the cruel taskmaster forcing you to do this?  If a variable is numeric, store it as numeric don't store it as character.  You might have to learn how to display it in a format for reporting purposes.  But it is senseless to continually jump through these conversion hoops.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 3 replies
  • 1648 views
  • 5 likes
  • 4 in conversation