全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
2690 2
2016-01-30
今天编程中用到了一个计算式

Y=(X)**(1/3)

也就是Y是X的1/3次方。

奇怪的是,当X为负数时,SAS无法计算。

比如X为-0.027,那么Y应该为-0.3,可是SAS不能计算。


我用的是SAS 9.3

有意思的是,我的三星手机自带的计算器也有同样的问题。倒是google可以计算这个。

各位是否可以试试?难道我的SAS有问题?怎么解决呢?

急用,在线等。多谢!
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2016-1-30 11:45:06
This is not surprising, because SAS calculate the answer with the LOG as below.  So LOG of a negative value is not accepted...  You have to
handle this yourself!   

        x ** y = exp(log(x)*y);

To bypass this problem, you need to write a macro in SAS. The macro looks like below:

From http://support.sas.com/kb/24/618.html

%macro nroot(invar,outvar,root);

  /* If the root is odd, then take the root of the absolute value and */
  /* multiply by the sign of the original value                       */
  if mod(&root,2)=1 then &outvar=sign(&invar)*abs(&invar)**(1/&root);
  else do;

    /* If the root is even and the original value is negative then */
    /* assign a missing value                                      */
    if &invar<0 then &outvar=.;

    /* Otherwise just take the root */
    else &outvar=&invar**(1/&root);
  end;
%mend nroot;

/* Use the macro */

data test;

  /* Generate some data */
  do i=1 to 10;
    x=rannor(123);
    y=x**3;

    /* Take the square root of y and store in y2root */
    %nroot(y,y2root,2);

    /* Take the cube root of y and store in y3root */
    %nroot(y,y3root,3);
    output;
  end;
run;

proc print data=test;
run;

Result:
Obs     i        x           y        y2root     y3root

      1     1    -0.32659    -0.03484     .         -0.32659
      2     2     1.54244     3.66963    1.91563     1.54244
      3     3     0.25903     0.01738    0.13183     0.25903
      4     4    -0.55583    -0.17173     .         -0.55583
      5     5     0.77872     0.47222    0.68718     0.77872
      6     6    -0.65520    -0.28127     .         -0.65520
      7     7    -0.02210    -0.00001     .         -0.02210
      8     8    -0.77265    -0.46126     .         -0.77265
      9     9     0.72423     0.37987    0.61634     0.72423
     10    10     1.26331     2.01620    1.41993     1.26331
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2016-1-30 17:23:35
Great!Merci!
However, formula like (-9)**(2) and (-9)**(-2) are calculable, while (-9)**(1/2) and (-9)**(-0.5) are not.

So if it is a negative number to the power of some value whose absolute value is less than 1 (taking a root), then SAS cannot calculate it.
If the implicit calculation is x ** y = exp(log(x)*y), then none of these should be calculable.

Why is that.......?

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群