Reputation: 21
Why is the following not yielding the desired result?
data data1;
do a = 0.0 to 1.0 by 0.1;
do b = 0.0 to 1.0 by 0.1;
do c = 0.0 to 1.0 by 0.1;
do d = 0.0 to 1.0 by 0.1;
if (a+b+c+d)=1 then output;
end;
end;
end;
end;
format a b c d 4.1;
run;
Upvotes: 2
Views: 569
Reputation: 63424
Numbers in SAS are stored in binary (as they are in most computing applications), and are often not precisely representing the decimal equivalent. Just like 0.33333333 != (1/3) exactly, neither do many 1/10ths represent their precise decimal value either.
data data1;
do a = 0.0 to 1.0 by 0.1;
do b = 0.0 to 1.0 by 0.1;
do c = 0.0 to 1.0 by 0.1;
do d = 0.0 to 1.0 by 0.1;
if round(a+b+c+d,.1)=1 then output;
end;
end;
end;
end;
format a b c d 4.1;
run;
Rounding fixes the issue.
You can read this SAS technical paper for more information.
Upvotes: 1
Reputation: 2320
I'm not really familiar with SAS, but in general when you have numbers like 0.1, it's represented in binary. Since .1 can't be represented exactly in binary then math equations don't always add up exactly. For instance 0.1 times 10 does not equal exactly 1.0 in floating point arithmetic. In general, don't use equality when working with floating point.
See http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
Upvotes: 1