BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
David_Billa
Rhodochrosite | Level 12

I've a character variable called calc_rslt and it has values like  -259328.943733,-35860.382829,0. Now I want to convert it to numeric without rounding off the values.

 

When I tried with the Format 22.6 in the Input function, I could see it is rounding off the value. e.g. -259328.943733 is rounded to -259328.9437.

 

calc_rslt_num = input(calc_rslt,22.6); 

Also if it is 0 (character) in the Input value then I want to convert it to 0.000000 (numeric). It is also not working when I tried with if then clause as shown below.

 

If value=0 then value=0.000000

Desired results for the provided values should be -259328.943733,-35860.382829,0.000000

 

Any help?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

There is a limit to the number of decimal digits that can be exactly represented (without gaps) using the binary floating point representation SAS (and most software) use to store numbers.  But your example strings are no where near that limit.

 -259328.943733
-35860.382829
0 

So to convert those strings into numbers use the normal numeric informat. 

calc_rslt_num = input(calc_rslt,32.); 

Note do NOT include a number of decimal places in the informat, unless you know that the periods have been removed to save space. Otherwise if you use 22.6 informat and the string does not contain a period the number is divided by 10**6 to place the implied decimal point.

 

If you want the value to display with 6 digits to the right of the decimal place the use a format when displaying the value.

format calc_rslt_num 22.6 ;

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

When you do this creation of a new numeric variable, it has a default format of best12. However, the full decimal places are there, which can be seen by changing the format.

 

data a;
    calc_rslt='-259328.943733';
    calc_rslt_num = input(calc_rslt,22.6); 
    format calc_rslt_num best24.;
run;

Of course, this begs the question about why you have non-integer numbers in a character variable, which doesn't seem like a good thing to do.

 

 

--
Paige Miller
David_Billa
Rhodochrosite | Level 12

@PaigeMiller how about my other question?

 

Also if it is 0 after converting from character to numeric then I want to convert it to 0.000000 (numeric). It is also not working when I tried with if then clause as shown in the Initial post

PaigeMiller
Diamond | Level 26

Internally, there is no difference between 0 and 0.000000, these are the same number. The only difference is the appearance, so you need to find a format (which has the effect of changing the appearance) that works on both -259328.943733 and 0.

--
Paige Miller
Tom
Super User Tom
Super User

There is a limit to the number of decimal digits that can be exactly represented (without gaps) using the binary floating point representation SAS (and most software) use to store numbers.  But your example strings are no where near that limit.

 -259328.943733
-35860.382829
0 

So to convert those strings into numbers use the normal numeric informat. 

calc_rslt_num = input(calc_rslt,32.); 

Note do NOT include a number of decimal places in the informat, unless you know that the periods have been removed to save space. Otherwise if you use 22.6 informat and the string does not contain a period the number is divided by 10**6 to place the implied decimal point.

 

If you want the value to display with 6 digits to the right of the decimal place the use a format when displaying the value.

format calc_rslt_num 22.6 ;

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
  • 20927 views
  • 2 likes
  • 3 in conversation