Asked • 04/28/19

SAS : Convert character to numeric without creating another variable?

I want to convert `x` to numeric. DATA test; input x $1.; cards; 1 2 0 ; run; I tried different ways : - With `*1` : /* trial1 */ DATA test1; SET test; x = x*1; run; The log prints the following note : NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column). 2470:3 NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column). 2470:4 And the format doesn't change. - With `input()` : /* trial2 */ DATA test2; SET test; x = input(x,BEST1.); run;` The log prints the following note : NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column). 2396:3 And the format doesn't change. - With `informat` : /* trial3 */ DATA test3; SET test; informat x BEST1.; run; The log prints the following error : ERROR 48-59: The informat $BEST was not found or could not be loaded. Which is explained [here](http://support.sas.com/kb/23/083.html) and [here](http://support.sas.com/resources/papers/proceedings11/262-2011.pdf) : the compiler detects different types for variable and format, assumes it's a mistake, add the presumed-missing `$` and therefore doesn't find the format. All these trials would work if I created a second variable like for example : DATA test4; SET test (rename=(x=x2)); x = x2*1; drop x2; run; But I'm trying to clean up my code and I wonder if there is a way to do such a conversion without doing so ?

1 Expert Answer

By:

Gerald B. answered • 06/14/19

Tutor
5 (3)

Experienced SAS User and Teacher

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.