DATA Step, Macro, Functions and more

converting numeric values to character and not losing the decimals

Accepted Solution Solved
Reply
Super Contributor
Posts: 441
Accepted Solution

converting numeric values to character and not losing the decimals

Hi,

 

I have a variable estimate with value = 14.793 and it is in numeric format 12.3 . I would like to convert it to character format and do the following:

 

estimate = put(estimate,$12.);

 

But the result that I get is 15.000, and the format is still 12.3. ?

 

 

Thank you!


Accepted Solutions
Solution
‎10-12-2016 10:33 PM
Super User
Posts: 3,260

Re: converting numeric values to character and not losing the decimals

estimate = put(estimate,12.3);

View solution in original post


All Replies
Solution
‎10-12-2016 10:33 PM
Super User
Posts: 3,260

Re: converting numeric values to character and not losing the decimals

estimate = put(estimate,12.3);
Super Contributor
Posts: 441

Re: converting numeric values to character and not losing the decimals

Wow, it works!!!

 

Its just that I was expecting the $ sign since I wanted to convert to character, but I guess that is some SAS peculiarity.

 

Thank a lot!

PROC Star
Posts: 1,760

Re: converting numeric values to character and not losing the decimals

$ is for formatting character variables. You format a numeric here.

Super User
Posts: 11,343

Re: converting numeric values to character and not losing the decimals

PUT creates character variables any time you use x= put (...) or x=Putn(...) or x=putc(...).

The format involved in the PUT portion controls what the resulting character varaible may look like. The format should match the variable type you are PUTting.

 

SAS will occasionaly help you by doing implicit conversion if you specify a numeric format for something that is character but looks numeric or specify a numeric variable and a character format though the results may not match what you expect.

Here is a brief example:

data junk;
   x= 25.4;
   y = put(x,$7.);
run;

Note that there is no Error but a warning is placed in the log and that the value of Y may not be as expected and has leading blanks.

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 326 views
  • 1 like
  • 4 in conversation