sas优化技巧(4)执行必要的部分where,if、select,if else、obs firstobs、读入外部数据时选择需要的obs(if+input)、keep/drop

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高效

where中能用contains的地方一律考虑用like

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个观测,证明是全部读入再一个个判断是否满足条件

 

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

2:select 、if else if的选择

For numeric variables, SELECT statements should always be slightly more efficient (use less CPU time) than IF-THEN/ELSE statements. The performance gap between IF-THEN/ELSE and SELECT statements gradually widens as the number of conditions increases

For character variables, IF-THEN/ELSE statements are always more efficient than SELECT statements. The performance gap widens quickly between the two techniques as the number of conditions increases.

 

使用两种选择方式的最佳情况

Use IF-THEN/ELSE statements when
􀀀 1:the data values are character values
􀀀 2:the data values are not uniformly distributed
􀀀 3:there are few conditions to check.

Use SELECT statements when
􀀀 1:you have a long series of mutually exclusive numeric conditions
􀀀 2:data values are uniformly distributed.

 

3:where和obs/firstobs连用,来选择需要的观测行

记住,obs/firstobs是逻辑上选择观测值,并不是实际选择也就意味着不会去除其他的观测值

where在obs/firstobs前执行

 

4:读入外部数据时选择需要的obs

先看一张外部数据读入后sas程序内部的流程图

这里不能用where来截取子集,因为where不能用于筛选外部数据。

这里将if放在input后筛选读入pdv的观测行来减少cpu的运行时间

I/O的次数指的是外部data和buffers之间的通信的次数,减少的方式肯定是不能通过where/if等语句,只可能通过bufno、bufsize、sasfile等选项

 

5:keep/drop

下图展示keep和drop对于数据的作用环节

buffer后的keep、drop可以减少输入pdv中的变量个数从而减少CPU time

buffer前的keep、drop可以减少读入buffer中的变量个数也就是读入数据的总大小,从而减少I/O次数

 

posted @ 2014-11-30 14:21  暴走的豆浆  阅读(5492)  评论(0编辑  收藏  举报