BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Sedrak
Calcite | Level 5

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;

1 ACCEPTED SOLUTION

Accepted Solutions
s_lassen
Meteorite | Level 14

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).

 

 

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

This is roundoff error, as computers cannot represent certain fractions exactly. You can use the ROUND() function or the FUZZ() function in this case.

 

if fuzz(one-two)^=0 then test=1;
--
Paige Miller
AMSAS
SAS Super FREQ

I recommend you review the documentation and search for Numeric Precision

 

yabwon
Meteorite | Level 14

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



s_lassen
Meteorite | Level 14

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).

 

 

SAS INNOVATE 2024

innovate-wordmarks-white-horiz.png

SAS is headed back to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team.

Interested in speaking? Content from our attendees is one of the reasons that makes SAS Innovate such a special event!

Submit your idea!

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 4 replies
  • 223 views
  • 11 likes
  • 5 in conversation