楼主: leonxf
2412 2

不可思议,SAS居然不能算这个?在线等 [推广有奖]

  • 0关注
  • 1粉丝

大专生

65%

还不是VIP/贵宾

-

威望
0
论坛币
1589 个
通用积分
0.8555
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
494 点
帖子
26
精华
0
在线时间
68 小时
注册时间
2009-1-31
最后登录
2024-3-21

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
今天编程中用到了一个计算式

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

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

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

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


我用的是SAS 9.3

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

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

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

扫码加我 拉你入群

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

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

关键词:不可思议 在线等 Google 三星手机 无法计算 SAS 计算 三星手机 计算器 google 在线

回帖推荐

jpang 发表于2楼  查看完整内容

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, ...
沙发
jpang 发表于 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
已有 1 人评分论坛币 信用等级 收起 理由
admin_kefu + 30 + 1 热心帮助其他会员

总评分: 论坛币 + 30  信用等级 + 1   查看全部评分

使用道具

藤椅
leonxf 发表于 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.......?

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-19 22:08