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
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
is any other way to retain the decimai numbers specifically 1.0 and 1.0000 during the conversion from character to numeric?
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.
You can count the number of decimals in the character string. What you do with that information is up to you.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.