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!
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.
Convert character to numeric
num_var=char_var+0;
Convert numeric to character
char_var = cats(num_var);
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.
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.
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!
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.