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;
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).
I recommend you review the documentation and search for Numeric Precision
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
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).
Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.
Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.
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.
Ready to level-up your skills? Choose your own adventure.