Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Programming
- /
- Programming
- /
- 0.4 < 0.4 is True?

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

☑ This topic is **solved**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 03-23-2023 07:11 AM
(653 views)

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

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

4 REPLIES 4

- 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=0011111111011001100110011001100110011001100110011001100110011001two=0011111111011001100110011001100110011001100110011001100110011010NOTE: 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

"

Hands-on-Workshop: "

"

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

Are you ready for the spotlight? We're accepting content ideas for **SAS Innovate 2025** to be held May 6-9 in Orlando, FL. The call is **open **until September 16. Read more here about **why** you should contribute and **what is in it** for you!

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.