楼主: qingww
5086 10

where and if statement区别 [推广有奖]

  • 0关注
  • 0粉丝

本科生

7%

还不是VIP/贵宾

-

威望
0
论坛币
0 个
通用积分
0
学术水平
0 点
热心指数
1 点
信用等级
0 点
经验
488 点
帖子
103
精华
0
在线时间
9 小时
注册时间
2009-7-8
最后登录
2010-11-20

楼主
qingww 发表于 2010-10-25 07:39:39 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
有谁能具体说说 WHERE 和 IF STATEMENT 的区别吗?
举个例子, 请教!! 谢谢!!
二维码

扫码加我 拉你入群

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

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

关键词:statement Statemen Statem Where State statement Where

沙发
pobel 在职认证  发表于 2010-10-25 08:09:04
一些例子,希望对你有点用处:

*** The general rule when comparing IF statement and WHERE statement is that when reading data from datasets, SAS first select observations based on the WHERE condition and then take other actions, while the IF statement would have to wait their turn. Using the WHERE statement may improve the efficiency of your SAS programs because SAS is not required to read all observations from the input data set.;

data test1;
     set sashelp.class;
     if sex="M";
run;

/*
log:
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.TEST1 has 10 observations and 5 variables.
*/

data test2;
    set sashelp.class;
    where sex="M";
run;

/*log:
NOTE: There were 10 observations read from the data set SASHELP.CLASS.
      WHERE sex='M';
NOTE: The data set WORK.TEST2 has 10 observations and 5 variables.
*/

*** When using two or more WHERE statements in the same DATA step, then which comes last rules.
     When using two or more IF statements, both(all) of them take effect;

*** Both of the two IF statements are applied to the data, TEST3 contains only female students whose age is less than 14;
data test3;
     set sashelp.class;
     if sex="F";
     if age<14;
run;

*** Only the latter WHERE statement takes effect and dataset test4 contains all students with age lt 14, irrespective of their gender.;
data test4;
    set sashelp.class;
    where sex="F";
    where age<14;
run;

*** Check out the following example;

data class;
     set sashelp.class;
run;

*** Using FIRSTOBS= and OBS= data set option;
*** SAS selects 9 records (from the second through the tenth);
data class1;
     set sashelp.class(firstobs=2 obs=10);
run;

*** Using WHERE statement;
*** SAS first select all female records, then try to set the 2-10 observations.;
data class2;
     set sashelp.class(firstobs=2 obs=10);
     where sex="F";
run;

*** Using IF statement;
*** SAS first select the 2-10 records, then select Female students;
data class3;
     set sashelp.class(firstobs=2 obs=10);
     if sex="F";
run;


*** Another example about IF condition and WHERE condition;
data temp;
     do x=1 to 10;
        output;
     end;
run;

*** Where condition;
data temp1;
    set temp;
    where '0';
run;

*** IF condition;
data temp2;
    set temp;
    if '0';
run;

*** The WHERE statement evaluated the '0' as true and selected all observations, while the IF statement evaluated '0' as false and select no observations.

The WHERE processor converted the character value directly to a logical value, the rule being that all-blank strings are false and any other strings are true. The IF statement triggered a more general sort of conversion in the DATA step, first converting the character string to a 0 (zero) numeric value, then interpreting that as false (the rule being that zeroes and missing values are false, while positive and negative values are true). ;

*** Small tip:
If you use both the WHERE= data set option and the WHERE statement in the same DATA step, SAS ignores the WHERE statement for data sets with the WHERE= data set option. ;
已有 2 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
yunqingwang + 1 + 1 + 1 对论坛有贡献
eijuhz + 100 精彩帖子

总评分: 论坛币 + 100  学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

和谐拯救危机

藤椅
Bridgenc 发表于 2010-10-25 08:24:16
已有 3 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
yunqingwang + 1 + 1 + 1 对论坛有贡献
pobel + 1 + 1 好的意见建议
eijuhz + 50 + 1 精彩帖子

总评分: 论坛币 + 50  学术水平 + 2  热心指数 + 3  信用等级 + 1   查看全部评分

板凳
jiayuan67 发表于 2010-10-25 13:56:47
Picture1.png

The variable used in the WHERE statement should exist in the input datasets. No such restrictions are for subsetting IF statement.




已有 2 人评分论坛币 学术水平 热心指数 收起 理由
pobel + 1 + 1 好的意见建议
eijuhz + 100 + 1 精彩帖子

总评分: 论坛币 + 100  学术水平 + 1  热心指数 + 2   查看全部评分

人生若只如初见

报纸
ryuuzt 发表于 2010-10-25 17:57:03
jiayuan67 发表于 2010-10-25 13:56


The variable used in the WHERE statement should exist in the input datasets. No such restrictions are for subsetting IF statement.



这个图很好啊。存起来备用。

地板
qingww 发表于 2010-10-26 03:09:08
thank you!!!

7
HYMa 发表于 2011-2-25 02:49:03
2# pobel
Good points!

8
yunqingwang 在职认证  发表于 2011-11-14 11:27:17
绝对的好贴

9
b00111309 发表于 2011-12-6 10:25:01
受启发了,不懂再来看看

10
greatvia 发表于 2012-1-27 17:30:46
en ..。。不错,算是很清楚了

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-27 06:31