It seems that I have a rounding issue with SAS PROC FORMAT
. The code is as below:
proc format;
value testf
-1.85E-13--1.85E-13 = 'negative'
0-0 = 'zero';
run;
data test;
input number best32.;
cards;
0
;
run;
data test2;
set test;
format varlabel $50.;
varlabel = put(number, testf.);
run;
The code is easy: first, create a format, with two options, a negative number really closed to zero and zero itself. Second, create dataset test, with a single observation whose number variable equ开发者_如何转开发als to 0. Third, create another dataset, with applied format. I expect to see test2 with number = 0 and varlabel = 'zero' but actually I see test2 with number = 0 and varlabel = 'negative'. Anyone knows why and how to fix this problem? Much appreciated for any suggestion/help.
This is a floating point precision problem. When testing floating point values for equality SAS employs a "fuzz" value of 1e-12 (by default). The expression a = b evaluates to true if abs(a-b) < 1e-12. In your case, 1.85e-13 is sufficiently close to zero that SAS will give it the format 'negative' (why it always assigns this and never 'zero' I don't know).
One way of handling this is to reduce the fuzz value SAS associates with your format:
proc format;
value testf (fuzz=1E-13) -1.85E-13--1.85E-13 = 'negative' 0-0 = 'zero';
run;
This format assigns the expected varlabel 'zero'.
If this sort of thing is a practical problem for you, you might want to think carefully about potential numeric precision issues in your code, as they're capable of generating some nasty and difficult-to-spot bugs in the wrong circumstances.
This link gives some information on numeric representation issues in SAS which may prove helpful.
精彩评论