Calcite | Level 5

## How to convert the decimal into character value

I have a numeric variable called AMOUNT and it has a value of 48.14 after I'm coverting it into a Character Variable called AMOUNT_CHAR but when I do that, the value appears as only 48. How do I convert the full value into character including the decimal as well.

Here's what I'm using.

AMOUNT_CHAR=COMPRESS(PUT(AMOUNT,\$10.));

Thanks

3 REPLIES 3
Quartz | Level 8

## Re: How to convert the decimal into character value

You need to mention proper format in PUT function.

try this:

``````data test;
AMOUNT = 48.14;
AMOUNT_CHAR=COMPRESS(PUT(AMOUNT,8.2));
run;``````
BI Developer. Writer. Creative Educator.

SAS Blog → https://learnsascode.com
Diamond | Level 26

## Re: How to convert the decimal into character value

@helios4122 wrote:

I have a numeric variable called AMOUNT and it has a value of 48.14 after I'm coverting it into a Character Variable called AMOUNT_CHAR but when I do that, the value appears as only 48. How do I convert the full value into character including the decimal as well.

Don't do this. A value of 48.14 is a number, the best programming practice is to leave it as a number.

--
Paige Miller
Super User

## Re: How to convert the decimal into character value

@helios4122 wrote:

I have a numeric variable called AMOUNT and it has a value of 48.14 after I'm coverting it into a Character Variable called AMOUNT_CHAR but when I do that, the value appears as only 48. How do I convert the full value into character including the decimal as well.

Here's what I'm using.

AMOUNT_CHAR=COMPRESS(PUT(AMOUNT,\$10.));

Thanks

You are running into several issues that I believe all stem from misunderstanding of Format involved syntax.

First you likely received a WARNING in the log such as is generated by the code shown below:

```1    data example;
2       amount = 48.14;
3       AMOUNT_CHAR=COMPRESS(PUT(AMOUNT,\$10.));
WARNING: Variable amount has already been defined as numeric.
4    run;
```

That Warning is because 1) the variable Amount is numeric and 2) you use a \$ format with it. So part of what happens, since you are attempting to Put with \$10 is SAS does an internal conversion to allow use of the \$10 format.

\$W format is for character values only. As such they never support decimal places as they do not belong in character values.

So you want to use a 10. , or W.D (W is total number of characters and D is the number of decimal points to include when displaying or Putting a numeric value. If you want to include exactly 2 decimal places for every single value of your Amount variable then the format to use would end in .2, could be 10.2, 8.2 or whatever makes sense.

However if some of your values have more decimal places they will get rounded with W.2 format to 2 places.

There is another artifact as well. Put with numeric formats by default will right justify the result. So with a value like 48.14 and 10.2 format the result will be 10 characters long and have 5 leading spaces by default.

If you want the result to be left justified you have to use the justification option in put:

`put(amount, 10.2 -L)`

Syntax and details over.

Now, what perceived advantage do you get by creating a character value from the Amount? It won't sort properly if left justified, it needs to be converted back to numeric for any calculation.

Discussion stats
• 3 replies
• 507 views
• 0 likes
• 4 in conversation