| 36 | 3.6 | -90 | 0 |
I expect a=1, flag=1. But I got zero, null value instead. Why?
How can I make SAS think it is -90?
It has probably something to do with numeric values in the SAS System being represented as IEEE double-precision floating-point numbers.
SAS Help Center: Numeric Precision
data _NULL_;
file print;
x=36; y=3.6;
z=(y-x)/x*100; put z= 25.15;
if z=-90 then do; put '1. hurray ; z equals -90'; end;
if int(z)=-90 then do; put '2. hurray ; z equals -90'; end;
if floor(z)=-90 then do; put '3. hurray ; z equals -90'; end;
if ceil(z)=-90 then do; put '4. hurray ; z equals -90'; end;
if round(z)=-90 then do; put '5. hurray ; z equals -90'; end;
run;
/* end of program */
Ciao,
Koen
@sbxkoenk is correct, this is a numerical precision issue. Here is more reading: Solved: comparing two values - SAS Support Communities and even more at https://communities.sas.com/t5/SAS-Programming/Formula-evaluates-totals-the-same-set-of-variables-di...
Your value of Z is not -90 because SAS cannot represent some decimals exactly. Decimal values that are not an integer power of 2 cannot be represented exactly. So y=3.6 produces a number that isn't quite 3.6 (can't represent it exactly) and so the value of Z (which uses Y) isn't exactly -90.
If you display the numbers using the HEX format so we can see the exact value, like this
data three;
x=36; y=3.6;
z=(y-x)/x*100;
minusninety=-90;
if z<=-90 then a=1; else a=0;
if a=-90 then flag=1;
run;
proc print data=three;
var x y z minusninety;
format x y z minusninety hex16.;
run;
this is the result, note that variable Z is not exactly equal to the value of variable MINUSNINETY. So then variable A=0 because it is not exactly equal.
Obs x y z minusninety 1 4042000000000000 400CCCCCCCCCCCCD C0567FFFFFFFFFFF C056800000000000
Then flag is missing because it only is assigned a value when A=1, but A=0 so no value is assigned.
I think the best solution is to use the ROUND function like this: if round(z)=-90 as suggest by @Kurt_Bremser here is a better solution which will work in more cases than the INT or FLOOR or CEIL function. In this case INT works but there may be other cases where you are not comparing to an integer, and so INT won't work in those cases but ROUND will.
Want to review SAS Customer Intelligence 360? Gartner and G2 are offering a gift card or charitable donation for each accepted review. Use this link for G2 to opt out of receiving anything of value for your review.
SAS Customer Intelligence 360
Assess your marketing efforts with a free tool
Training Resources
SAS Customer Intelligence Learning Subscription (login required)
SAS' Peter Ansbacher shows you how to use the dashboard in SAS Customer Intelligence 360 for better results.
Find more tutorials on the SAS Users YouTube channel.
Want to review SAS Customer Intelligence 360? Gartner and G2 are offering a gift card or charitable donation for each accepted review. Use this link for G2 to opt out of receiving anything of value for your review.
SAS Customer Intelligence 360
Assess your marketing efforts with a free tool
Training Resources
SAS Customer Intelligence Learning Subscription (login required)