Calcite | Level 5

```%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
Lapis Lazuli | Level 10

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

Super User

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.

Calcite | Level 5

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

@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.:

Super User