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: addition of variables

Options

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

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

Posted 08-01-2019 04:59 AM
(889 views)

%let z=-.4; data new; x=2.05000; x1=2.45; a=0 < x < &z.+x1; run;

when I am running this code the value of variable a in dataset new is coming 1 why ?

I think it should come as 0

5 REPLIES 5

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

. The result of a comparison operation is 1 if the comparison is true and 0 if it is false.

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

Let's go through this step by step:

```
%let z=-.4;
data new;
x = 2.05000;
x1 = 2.45;
a = 0 < x < &z. + x1;
run;
```

First, the macro variable is resolved:

```
data new;
x = 2.05000;
x1 = 2.45;
a = 0 < x < -.4 + x1;
run;
```

Next, the calculation is done:

```
data new;
x = 2.05000;
x1 = 2.45;
a = 0 < x < -.4 + 2.45;
run;
```

The calculation involves a decimal fraction, which is often a big thing with SAS, because of numeric precision:

```
data test;
x1 = -.4 + 2.45;
x2 = 2.05;
x3 = x1 - x2;
format x3 best32.;
run;
proc print data=test noobs;
run;
```

Result:

x1 x2 x3 2.05 2.05 4.4408920985006E-16

So let's get back to your original code, and prudently apply the round() function to the calculation:

```
%let z=-.4;
data new;
x = 2.05000;
x1 = 2.45;
a = 0 < x < round(&z. + x1,.001);
run;
```

You will see that now you get your intended result.

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

thanks for your answer .it works correctly

But I have a question why decimal fraction is behaving like this and what does round function has done in this case

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

@shubham1 wrote:

But I have a question why decimal fraction is behaving like this and what does round function has done in this case

Hi @shubham1,

A short answer is: Internally, the computer doesn't use the decimal system, but the binary system. The vast majority of "short" decimal fractions (such as 0.1, 1.234, etc.) have *infinitely* many *binary* digits (quite similar to 1/3=0.3333333... in the decimal system), but need to be stored in a *finite* number of bits in the computer's memory. The unavoidable rounding errors due to this limitation can add up to larger errors in calculations. The ROUND function removes the additional error because this error is still much smaller than the rounding unit in the second argument, here: .001.

For more details and examples, please see the documentation Numerical Accuracy in SAS Software and/or other discussions on here about this topic (where I've contributed detailed explanations), e.g.:

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

SAS stores all numbers as 8-byte real (binary).

Most decimal fractions cannot be exactly represented in binary, and calculations cause additional aberrations like the one you experienced. Rounding gets the original imprecision back, so the compare for equal works again.

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. **Registration is now open through August 30th**. Visit the SAS Hackathon homepage.

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.