您的位置:首页 > 其它

sas条件判断语句where,if的区别,以及where选项

2014-07-03 15:26 633 查看
1:where和If最本质的区别,以及一些小的区别

1.1:The WHERE statement examines what is in the input page buffer and selects observations before they are loaded in the program data vector, which results in a savings in CPU operations(Where从buffer中进行筛选再读入pdv)

The subsetting IF statement loads all observations sequentially into the program data vector. If the statement finds a match and the statement is true, then the data is processed and is written to the output page buffer(If先读入pdv再进行筛选)

1.2:if可以从input的数据和sas数据集的数据中进行筛选,where只能筛选sas数据集的数据

if可以if语句的条件条件选择子句,where不能



where比if高效

if语句<可执行语句>

IF statement tells SAS which observations to include, the DELETE statement tells SAS which observations to exclude

IF Sex = 'f'; IF Sex = 'm' THEN DELETE; 作用一样!

data b;
set sashelp.class;
if _n_ le 4;  *如果if为真,则继续执行if后面的语句,最后输出满足if的条件的观测,如果if为假则立刻返回到data步开头继续执行下一条set语句;
y = 'now';
  /*
  y = 'now';
  if _n_ le 4;也能得出同样的结果,但是效率相对来说较低,因为要重复执行y的赋值语句
*/
run;

if的另外两种格式
if x=3 then y=4;  对于要表达的只有一条数据就用then
if x=3 then do y=4;z=5;end;  对于要表达的有多条语句就用then do end;

NOTE: 从数据集 SASHELP.CLASS. 读取了 19 个观测
NOTE: 数据集 WORK.B 有 4 个观测和 6 个变量。
NOTE: “DATA 语句”所用时间(总处理时间):
实际时间 0.03 秒
CPU 时间 0.03 秒 日志中读入了19个观测,证明是全部读入再一个个判断是否满足条件

[b] where语句(where=选项)<不可执行语句>[/b]

是在系统准备把观测读入pdv之前制定数据必须满足的一个条件。

WHERE where-expression-1<logical-operator where-expression-n>;

logical-operator can be AND, AND NOT, OR, or OR NOT

where表达式的算符:between and、is missing (is null)、contain (?)、like、same and、in

TIPS:

  1:where语句不能与自动变量连用以及新创建的变量连用,因为where语句在pdv之前执行

  2:使用where语句时,必须保证读入数据集的完整性,不能使用firstobs=2等不能完整读入数据集的选项

  3:当where选项与where语句同时作用于一个数据集的时候,系统只会考虑where选项,where选项可以只对某一个数据集起作用,而where语句是对所有的数据集起作用。

  4:当data步包含where语句和by语句时,where语句先于by语句之前被执行,by组对执行完毕后的数据集重新定义first/last变量。

  5:能用contains的地方都能用like、所以首先考虑用like。where x like a_b%; '_'表示正好有一个字符与之匹配,'%'表示可以替代任意多个字符

data a;
input x y@@;
cards;
1 10 1 20 0 200 2 30 2 40
3 50 3 60 4 70 3 80 4 400
;
run;

proc sort data=a;by x;run;
data b;
set a;
*where x ; *后面不添加条件是筛选x不为0和不为缺失值的数值型数据,只适用于数值型;
where x is not missing; *筛选x不为缺失值的数据包括0,适用于数值型和字符型;
run;

proc print data=b noobs;


where和if的最重要的几点区别

1:where不可执行、if可执行

2:where有自己特定的表达式,if是是通用表达式 例如where x is missing;

3:where只能从现有的sas数据集中选择观测,if语句还可以用input语句产生的观测中选择。*商用的一般都是现有的sas数据集;

4:where的效率比if高

5:何时使用if何时使用where?如果需要对pdv观测进行处理才能决定哪条观测,只能使用if。其余能使用where
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐