sas-常用的where語句必知知識點

where語句實在執行數據連接(set),合併(merge),更新(update)或者修改之前的操作,使用where語句時,應為sas系統只是從輸入的數據集中讀入滿足條件的觀測,所以這樣的SAS程序更加有效。



  • where語句的優缺點:
  1. where語句不是可執行語句,它起不到if-then語句的作用;
  2. 能用where語句的地方,一定可以用if語句來代替,反之則不行;
  3. 一旦where語句有效,最好就用where語句,因為這樣的程序效率高。
  • 僅僅用於where表達式的特殊算符:
  1. between-and 選擇一定數值範圍內的觀測
  2. is missing|is null 選擇變量值為缺失值的所有觀測
  3. contains|? 選擇包含規定字符串的觀測
  4. like 匹配選擇觀測
  5. 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>


分享到:


相關文章: