- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hey everyone. I have this code, which creates two variables one, two, which both are equal to 0.4, but for some reason one = 0.4 but one < 0.4 is true. Can somebody explain this?
data test;
one = 1.9/4.75;
two = 4/10;
if one < two then test = 1;
run;
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
The error comes because SAS numbers are represented internally as base 2 floating-point numbers. The numbers which cannot be represented exactly in base 2 in your program are 1.9 and 0.4, both of which are approximate when stored in SAS as numbers.
So one approximate number is calculated as approximately 1.9, divided by 19, and multiplied by 4 (4.75 is represented exactly as 19*2^-2), whereas the other approximate number is calculated by dividing 4 by 10.
This is the reason why you sometimes have problems doing accounting in SAS - once in a while you get some rounding errors, which do not matter at all in scientific calculations or in reporting, but in accounting it means that something is wrong somewhere. Databases and languages more suited for accounting and transactions have a fixed decimal type, which does not have these problems. An in most object-oriented languages you can find classes for rational numbers, which may also work correctly in this case (provided that 1.9 is understood/entered as 19/10, and not a base 2 floating-point approximation).
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I recommend you review the documentation and search for Numeric Precision
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Agree with @AMSAS suggestion.
As an additional exercise check out this:
1 data test; 2 one = 1.9/4.75; 3 two = 4/10; 4 5 if one < two then test = 1; 6 7 put (one two) (= binary64. /); 8 run; one=0011111111011001100110011001100110011001100110011001100110011001 two=0011111111011001100110011001100110011001100110011001100110011010 NOTE: The data set WORK.TEST has 1 observations and 3 variables. NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.00 seconds
Bart
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug
"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings
SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
The error comes because SAS numbers are represented internally as base 2 floating-point numbers. The numbers which cannot be represented exactly in base 2 in your program are 1.9 and 0.4, both of which are approximate when stored in SAS as numbers.
So one approximate number is calculated as approximately 1.9, divided by 19, and multiplied by 4 (4.75 is represented exactly as 19*2^-2), whereas the other approximate number is calculated by dividing 4 by 10.
This is the reason why you sometimes have problems doing accounting in SAS - once in a while you get some rounding errors, which do not matter at all in scientific calculations or in reporting, but in accounting it means that something is wrong somewhere. Databases and languages more suited for accounting and transactions have a fixed decimal type, which does not have these problems. An in most object-oriented languages you can find classes for rational numbers, which may also work correctly in this case (provided that 1.9 is understood/entered as 19/10, and not a base 2 floating-point approximation).