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

I have a number that after removing all formats and informats appears as 50.0000 but behaves like it is less than 50.  Any suggestions as to what may be happening here?  

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

I think this is a numerical precision problem. There are a few ways to deal with it. You could round the variable to the nearest integer and so on. 

View solution in original post

5 REPLIES 5
PeterClemmensen
Tourmaline | Level 20

I think this is a numerical precision problem. There are a few ways to deal with it. You could round the variable to the nearest integer and so on. 

Reeza
Super User
If you want it to be 50, try applying the ROUND() function.

ROUND(varName, 0.0001)
Patrick
Opal | Level 21

This can happen when reading a floating point number from another environment (i.e. a database). It's not a SAS problem but a generic challenge when exchanging such numbers between platforms. It's only in the last digits and the way around this is to round the number in the target environment to something of higher precision than what you actually expect the numbers to be. I.e. if you expect to get numbers with up to 4 decimals then use a round() function and round up to the 5th decimal.

data test;
  var=50 - (1/10**14);
  var=round(var,.0000000000001);
  if var=50 then output;
run;
proctice
Quartz | Level 8

Really annoying.  But, I guess some conditional rounding is the only answer.   Thanks.   

FreelanceReinh
Jade | Level 19

Data transfers between different environments (as mentioned by Patrick) are one source of this kind of (numerical accuracy) issues; calculations with non-integer values are another. Here's an example (SAS log under Windows 7):

1    data _null_;
2    x=9.9/3.3; /* Obviously, x should be exactly 3. */
3    if x>3 then put 'Surprise!' / x= 32.30; /* Standard formats are deceptive. */
4    d=x-3;
5    put d=; /* The difference is tiny, but not zero. */
6    r=round(x, 1e-9); /* Appropriate rounding "adjusts" the number to what it should be. */
7    put (x r) (hex16. /); /* HEX and BINARY formats tell the truth. */
8    run;

Surprise!
x=3.000000000000000000000000000000
d=4.440892E-16
4008000000000001
4008000000000000

Note that the standard w.d format (32.30) actually rounds the value so that the deviation from 3 is not shown, although the internal value is (after binary-to-decimal conversion) 3.000000000000000444089...

 

It's good practice to correct such values as soon as they occur (e.g. after a data transfer) or to avoid them in the first place (if possible), e.g., by using the ROUND function in calculations which otherwise are prone to those issues.

 

See Numerical Accuracy in SAS Software for background information.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 1351 views
  • 0 likes
  • 5 in conversation