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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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