我似乎对 SAS PROC FORMAT 有舍入问题
我似乎对 SAS PROC FORMAT
存在舍入问题。代码如下:
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;
代码很简单:首先,创建一个格式,有两个选项,真正接近于零的负数和零本身。其次,创建数据集测试,其中包含一个数字变量等于 0 的观察值。第三,使用应用的格式创建另一个数据集。我希望看到 test2 的 number = 0 和 varlabel = 'zero',但实际上我看到 test2 的 number = 0 和 varlabel = 'negative'。有人知道为什么以及如何解决这个问题吗?非常感谢任何建议/帮助。
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 equals 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个浮点精度问题。当测试浮点值是否相等时,SAS 使用“模糊”值 1e-12(默认情况下)。如果 abs(ab) < ,则表达式 a = b 计算结果为 true 1e-12。在你的情况下,1.85e-13足够接近零,SAS会给它格式“负”(为什么它总是分配这个而不是“零”,我不知道)。
处理此问题的一种方法是减少 SAS 与您的格式关联的模糊值:
此格式分配预期的 varlabel“零”。
如果这类事情对您来说是一个实际问题,您可能需要仔细考虑代码中潜在的数字精度问题,因为它们能够在错误的情况下生成一些令人讨厌且难以发现的错误。
此链接提供了有关 SAS 中数字表示问题的一些信息,这些信息可能会有所帮助。
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:
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.