%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
. The result of a comparison operation is 1 if the comparison is true and 0 if it is false.
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.
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
@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.:
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.
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 25. Read more here about why you should contribute and what is in it for you!
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.