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
- /
- Re: Apparent SAS calculation error

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** and **locked**.
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 02-18-2021 10:02 AM
(556 views)

Consider the following code. a = b and yet SAS determines that a < b. Why?

data x;

a = 11*10;

b = (11/10)*100;

if a = b then put 'equal';

if a > b then put 'greater';

if a < b then put 'less';

output;

run;

1 ACCEPTED SOLUTION

Accepted Solutions

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

Do a google search for "sas numeric precision", and you will find the answer.

In short: decimal fractions cannot be exactly represented as binary data, so you introduce minimal aberrations during the calculation. When testing for equality, always use the ROUND() function before you compare.

BTW 11/10 can't even be represented exactly as a *decimal* value, as it resolves to 1.1 periodic.

3 REPLIES 3

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

Do a google search for "sas numeric precision", and you will find the answer.

In short: decimal fractions cannot be exactly represented as binary data, so you introduce minimal aberrations during the calculation. When testing for equality, always use the ROUND() function before you compare.

BTW 11/10 can't even be represented exactly as a *decimal* value, as it resolves to 1.1 periodic.

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

Computers cannot store numbers exactly.

Almost all software applications can generate such an error.

https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.5&docsetId=lrcon&docsetTarget=p0ji1u...

Almost all software applications can generate such an error.

https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.5&docsetId=lrcon&docsetTarget=p0ji1u...

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

Hello @SAS1ns1ghts,

See this 2020 post for a detailed explanation why for SAS (under Windows) 0.07*100>7 (!). Your example, 1.1*100>110, is quite similar. The length of the period in the binary (periodic) fraction representing the decimal fraction 1.1 is 4, not 20 as in the case of 0.07, but the basic pattern is the same: a periodic fraction is rounded internally and the rounding error propagates in calculations.

**Available on demand!**

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

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.