How to convert to char from numeric without losing decimals

Solved
Frequent Contributor
Posts: 82

How to convert to char from numeric without losing decimals

Hello,

I have a numeric variable x and i tried to convert to character using: put (x, best32.).

Now, x can have values like 3, 3.0, 3.02 etc...upto 2 decimal places.

How to keep the format as is..i.e 3 -->3, 3.0 --> 3.0 and so on?

Currently, with my code, it is outputting everything as 1 decimal places...and dropping the second decimal.

Thanks very much

Accepted Solutions
Solution
‎03-06-2015 12:43 PM
Super User
Posts: 11,343

Re: How to convert to char from numeric without losing decimals

It may be that

```with my code, it is outputting everything as 1 decimal
```

is due to the method. Some procedures round for simplicity. Show how you are creating that output.

If you are printing the output then the format is likely an issue. Try assigning a format that forces 2 decimals such as

Format variable f8.2 ;

If the default format is something like best8. and you have values of 12345678.99 then only 8 digits are likely to display and the .99 wouldn't be seen.

All Replies
Super User
Posts: 7,942

Re: How to convert to char from numeric without losing decimals

Hi,

Just specify the number of decimal places:

data have;

num=3; output;

num=3.01; output;

num=3.1; output;

run;

data want;

set have;

length text \$10.;

text=strip(put(num,best8.2));

run;

Solution
‎03-06-2015 12:43 PM
Super User
Posts: 11,343

Re: How to convert to char from numeric without losing decimals

It may be that

```with my code, it is outputting everything as 1 decimal
```

is due to the method. Some procedures round for simplicity. Show how you are creating that output.

If you are printing the output then the format is likely an issue. Try assigning a format that forces 2 decimals such as

Format variable f8.2 ;

If the default format is something like best8. and you have values of 12345678.99 then only 8 digits are likely to display and the .99 wouldn't be seen.

Frequent Contributor
Posts: 82

Re: How to convert to char from numeric without losing decimals

Sorry, but 3.10 is coming as 3.1, the last 0 is getting dropped.

data have;

num=3; output;

num=3.01; output;

num=3.10; output;

run;

data want;

set have;

length text \$10.;

text=strip(put(num,best7.2));

run;

Super User
Posts: 7,942

Re: How to convert to char from numeric without losing decimals

Yes, you can't fix set number of numbers and not use it for all the values like that.  So, to fix it to two decimal output you would use z8.2, however the upshot of this is that 3 would become 3.00.  That is I am afraid consistency.  If you don't want that effect then you will have to pad it yourself in the text field, ie.

temp=strip(substr(index(text,".")+1));  /* This gets you the decimal part */

if length(strip(temp)) < 2 then text=cats(strip(text),repeat("0",length(temp)-1);

Super User
Posts: 7,039

Re: How to convert to char from numeric without losing decimals

The number 3.1 and the number 3.10 are the exact same number.  If you want to distinguish between them you will need to store them differently.

You could store them in a character variable.

Perhaps store the display format you want :  (3.1, 'F3.1') and (3.10,'F4.2')

Or just the number of decimal places (3.1, 1) and (3.10, 2).

You could store them in different variables which could have their own formats attached.

DECIMAL1 = 3.1;

DECIMAL2 = 3.1;

format decimal1 F3.1 decimal2 F4.2 ;

🔒 This topic is solved and locked.