全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
2318 9
2010-11-03
今天逛博,无意中看到下面一道趣题,关于宏的:
%macro aa;
%let condition=( 1.2>=1 and 1.2<1.5 ) and ( 1123>=800 and 1123<1200 );

   %if &condition %then %do;
    %put x;
   %end;
   %else %do;
%put M;
   %end;

%let condition=(( 1.2>=1 and 1.2<1.5 ) and ( 1123.1>=800 and 1123.1<1200 ));
   %if &condition %then %do;
    %put Y;
   %end;
   %else %do;
%put N;
   %end;

   %mend aa;
%aa;
跑了下 结果是X N  
很有意思,大家能帮忙解释下吗?
二维码

扫码加我 拉你入群

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

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

全部回复
2010-11-3 23:47:01
1# sopching


For 2nd condition (1123.1>=800) is FALSE because 1<8 in the character order, and thus the 2nd condition does not meet.
Using %sysevalf(&condition) may get really 'true' result as you expected.
JingJu
二维码

扫码加我 拉你入群

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

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

2010-11-4 00:45:16
2# jingju11
那为什么第一个条件的 不用%sysevalf?1.2<1.5是如何成立的?
二维码

扫码加我 拉你入群

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

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

2010-11-4 00:48:47
sopching 发表于 2010-11-4 00:45
2# jingju11
那为什么第一个条件的 不用%sysevalf?1.2
1=1, .=., 2<5 so 1 . 2 < 1 . 5
二维码

扫码加我 拉你入群

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

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

2010-11-4 00:53:08
赞啊,大师果然厉害!
二维码

扫码加我 拉你入群

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

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

2010-11-4 07:42:27
这个问题的关键在于%IF和%EVAL的运行机制,

%if语句会自动将%eval函数用在%if后的条件上,

%eval的运行:
If all operands can be interpreted as integers, the expression is treated as arithmetic. If at least one operand cannot be interpreted as numeric, the expression is treated as logical. If a division operation results in a fraction, the fraction is truncated to an integer.

所以,如果作为%if的条件,(1123>=800) 的结果就是TRUE(数学运算),而(1123.1>=800)的结果就是FALSE(字符比较)。

例如:
%macro test;
   %if 12>5 %then %put This is OK!;
   %else %put This is not OK!;
   %if 12.0>5.0 %then %put This is right!;
   %else %put This is wrong!;
%mend;

%test
二维码

扫码加我 拉你入群

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

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

点击查看更多内容…
相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

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