where语句实在执行数据连接(set),合并(merge),更新(update)或者修改之前的操作,使用where语句时,应为sas系统只是从输入的数据集中读入满足条件的观测,所以这样的SAS程序更加有效。
- where语句的优缺点:
- where语句不是可执行语句,它起不到if-then语句的作用;
- 能用where语句的地方,一定可以用if语句来代替,反之则不行;
- 一旦where语句有效,最好就用where语句,因为这样的程序效率高。
- 仅仅用于where表达式的特殊算符:
- between-and 选择一定数值范围内的观测
- is missing|is null 选择变量值为缺失值的所有观测
- contains|? 选择包含规定字符串的观测
- like 匹配选择观测
- same -and 增加多个从句
<code>where age between 18 and 25;
where taxes between salary*0.3 and salary*0.7;
where age is missing;
where name like 'D_AN';
where name like 'd_an%'
where x in (1,2,3,4,5);
where=(age in (1,2,3,4,5);
where=(age=8);/<code>
- where和子集if 语句的比较
在data步中where语句和子集if语句最大从差别就是where在观测读入到程序向量之前起作用,而子集if语句对已经在程序数据向量的观测起作用。where不是执行语句,子集If是可执行语句。
从大的SAS数据集中选择一个小在子集时用WHERE语句会币IF语句效率高很多,因为在进行选择之前SAS没有从大数据集中一定所有的观测到小的数据集效率中。
<code>data a;
set hah;
if _n_<100;
run;
data a;
set hah;
where _n_<100;
run;
/*错误语句,必须使用IF,where语句不能控制Sas的自动变量*//<code>
閱讀更多 數據小鄭 的文章