SAS按增量循环

时间:2014-01-28 18:58:24

标签: sas

为什么以下内容没有产生预期效果?

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;

2 个答案:

答案 0 :(得分:1)

我对SAS并不熟悉,但一般来说,当你的数字为0.1时,它用二进制表示。由于.1不能完全用二进制表示,因此数学方程式并不总是完全相加。例如,0.1乘以10在浮点运算中不等于1.0。通常,在使用浮点时不要使用相等。

请参阅http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

答案 1 :(得分:1)

SAS中的数字以二进制形式存储(因为它们在大多数计算应用程序中),并且通常不能精确地表示十进制等效值。就像0.33333333!=(1/3)完全一样,许多1/10也不代表它们的精确十进制值。

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解决了这个问题。

您可以阅读this SAS technical paper了解详情。