BookmarkSubscribeRSS Feed
Senthil1102
Calcite | Level 5

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

4 REPLIES 4
Tom
Super User Tom
Super User

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

Senthil1102
Calcite | Level 5

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

jbaldwin
Obsidian | Level 7

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.

data_null__
Jade | Level 19

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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 12142 views
  • 0 likes
  • 4 in conversation