全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
6302 5
2012-08-23
QUESTION 111
A SAS PRINT procedure output of the WORK.LEVELS data set is listed below:
Obsname level
1 Frank 1
2 Joan 2
3 Sui 2
4 Jose 3
5 Burt 4
6 Kelly .
7 Juan 1
The following SAS program is submitted:
data work . expertise;
set work. levels;
if level = . then expertise = 'Unknown';
else if level = 1 then expertise = 'Low';
else if level = 2 or 3 then expertise =' Medium';
else expertise = 'High';
run;
Which of the following values does the variable EXPERTISE contain?
A. Low, Medium, and High only
B. Low, Medium, and Unknown only
C. Low, Medium, High, and Unknown only
D. Low, Medium, High, Unknown, and ' ' (missing character value)
Answer: B
虽然我运行出来的结果也是B但不理解为什么读入4后是medium不是high啊?求高手解答!
二维码

扫码加我 拉你入群

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

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

全部回复
2012-8-23 15:48:53
else if level = 2 or 3 then expertise =' Medium';

这句是个陷阱--你测试一下下面的结果


data btest;
  zor=1 or 2 or 3 or 4 or 5;
  z_or=        (1 or 2 or 3 or 4 or 5);
  zand=        1 & 2 &3 & 4 & 5;
  z_and= (1 & 2 &3 & 4 & 5);
  z_and_1=(1=        1 & 2 &3 & 4 & 5);
  z_and_2=(2=        1 & 2 &3 & 4 & 5);
  z_and_3=(3=        1 & 2 &3 & 4 & 5);
  z_and_4=(4=        4 & 2 &3 & 4 & 5);
  z_and_5=(5=        5 & 2 &3 & 4 & 5);
  x1=(1=2 or 3);
  x2=(1=(2 or 3));
  x3=(2=2 or 3);
  x4=(2=(2 or 3));
  y1=(1=2 & 3);
  y2=(1=(2 & 3));
  y3=(2=2 & 3);
  y4=(2=(2 & 3));
  y5=(3=2 & 3);       
  put _all_;
  run;



你看一下sas-log


1    data btest;
2      zor=1 or 2 or 3 or 4 or 5;
3      z_or= (1 or 2 or 3 or 4 or 5);
4      zand= 1 & 2 &3 & 4 & 5;
5      z_and= (1 & 2 &3 & 4 & 5);
6      z_and_1=(1=   1 & 2 &3 & 4 & 5);
7      z_and_2=(2=   1 & 2 &3 & 4 & 5);
8      z_and_3=(3=   1 & 2 &3 & 4 & 5);
9      z_and_4=(4=   4 & 2 &3 & 4 & 5);
10     z_and_5=(5=   5 & 2 &3 & 4 & 5);
11     x1=(1=2 or 3);
12     x2=(1=(2 or 3));
13     x3=(2=2 or 3);
14     x4=(2=(2 or 3));
15     y1=(1=2 & 3);
16     y2=(1=(2 & 3));
17     y3=(2=2 & 3);
18     y4=(2=(2 & 3));
19     y5=(3=2 & 3);
20     put _all_;
21     run;


zor=1 z_or=1 zand=1 z_and=1 z_and_1=1 z_and_2=0 z_and_3=0 z_and_4=1 z_and_5=1 x1=1 x2=1 x3=1 x4=0
y1=0 y2=1 y3=1 y4=0 y5=0 _ERROR_=0 _N_=1


这样你就看出了   level = 2 or 3  不管leve=1,2,3,4,都是真的,下面的else是不会执行的


明白?


二维码

扫码加我 拉你入群

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

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

2012-8-23 16:01:24
if level = 2 or 3  这个表达式不等价于  if level = 2 or level=3
二维码

扫码加我 拉你入群

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

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

2012-8-23 16:12:11
6738431 发表于 2012-8-23 15:48
else if level = 2 or 3 then expertise =' Medium';

这句是个陷阱--你测试一下下面的结果
是变成判断if (level=2) or 3 的意思吗?无论(level=2)值为0还是1 or3后最后值都是1 真的意思吗?
二维码

扫码加我 拉你入群

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

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

2012-8-23 16:12:52
ziyenano 发表于 2012-8-23 16:01
if level = 2 or 3  这个表达式不等价于  if level = 2 or level=3;
显然不是。 2 or 3 是一个表达式,它的值与level 比较····给你程序代码了,你运行一下看看就明白了
二维码

扫码加我 拉你入群

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

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

2012-8-23 16:19:37
6738431 发表于 2012-8-23 16:12
显然不是。 2 or 3 是一个表达式,它的值与level 比较····给你程序代码了,你运行一下看看就明白了
对哦,逻辑运算 从左向右运行
二维码

扫码加我 拉你入群

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

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

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

说点什么

分享

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