New Contributor
Posts: 3

# converting character to numeric variable

i had a character variable with the following values...
1.0
2.123
1.222
1.0000
3.1022

while converting it to numeric variable, the trailing zeros are disappeared...is there any way to retain the exact decimal numbers while converting from character to numeric variable...

1

2.123

1.222

1

3.1022

Super User
Posts: 8,111

## Re: converting character to numeric variable

SAS stores numbers as floating point, so there is no difference in how it is stored between the numbers 1.0 and 1.0000 in your example input stream. You can use a FORMAT to tell SAS how to display the number, but the same format will be applied to every observation in the data. For your example data you could use the format 6.4 (which can also be written as F6.4).

data test;

input char \$ @@;

num = input(char,6.4);

put char= num= num= 6.4 ;

cards;

1.0 2.123 1.222 1.0000 3.1022

run;

char=1.0 num=1 num=1.0000

char=2.123 num=2.123 num=2.1230

char=1.222 num=1.222 num=1.2220

char=1.0000 num=1 num=1.0000

char=3.1022 num=3.1022 num=3.1022

New Contributor
Posts: 3

## Re: converting character to numeric variable

is  any other way to retain the decimai numbers  specifically 1.0 and 1.0000  during the conversion from character to numeric?

Occasional Contributor
Posts: 13

## Re: converting character to numeric variable

To follow-on what data_null_ stated ("What you do with that information is up to you.") I think it would be helpful to describe why you want to do this.  It would seem that if you need it both numeric and character, then you keep both variables and use the one that's needed at the time.

For example if the objective is to line up the decimal places so that you get something like

1

1.000

31.2

0.00012220000

Then you would need to create an additional character string with a fixed length and the decimal would always show in the same position of the character string.

Posts: 3,852

## Re: converting character to numeric variable

You can count the number of decimals in the character string.  What you do with that information is up to you.

data dec;
input c \$;
if index(c,'.')

then d = length(scan(c,-1,'.'));
else d = 0;
num = input(c,
f12.);
cards;
1.0
2.123
1.222
1.0000
3.1022
;;;;
run;
Discussion stats
• 4 replies
• 6377 views
• 0 likes
• 4 in conversation