where 语句或 where 数据集选项哪个更快
Which is faster, where statement or where data set option
问题很直接,哪个更快?
考虑到我们在 set 语句中对两个数据集使用数据步骤,并且数据集中具有相同的变量。
根据我所听到和阅读的内容,如果我们使用相同的条件对它们进行子集化,比如 date = "10jan2014"d, the result will be exactly the same using the statement above or the following data set option in the two datasets (where=(date="10jan2014"d))。因为 where 是在任何东西到达 PDV 之前执行的。
对吗?
为了更好地理解问题,我创建了以下代码:
假设我们有这两个数据集。
data people1;
format birth date9.;
input name $ birth :date9.;
datalines;
John 18jan1980
Mary 20feb1980
;
run;
data people2;
format birth date9.;
input name $ birth :date9.;
datalines;
Peter 18mar1980
Judas 18jan1980
;
run;
我想用这两个创建一个新数据集,但只适用于出生日期等于 18jan1980 的人。
现在我可以使用 where 语句或 where 数据集选项。
使用 where(数据集选项):
data everybody1;
set people1 (where=(birth="18jan1980"d))
people2 (where=(birth="18jan1980"d));
run;
使用 where 语句:
data everybody2;
set people1
people2;
where birth="18jan1980"d;
run;
新数据集现在具有完全相同的输出。但是哪个更快?
谢谢。
它们在性能方面是相同的。是的,where 将只允许将匹配结果加载到 PDV。
根据文档 (http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000202951.htm):
The WHERE statement selects observations before they are brought into
the program data vector, making it a more efficient programming
technique.
上面的引述是在谈论 where
与 if
语句,但它确实确认 where
子句不会将数据带入 PDV。
编辑:感谢 Joe 的 link,我将提供一个更好的文档摘录 (http://support.sas.com/documentation/cdl/en/lrcon/67885/HTML/default/viewer.htm#p04fy20d8il3nfn1ssywe4f25k27.htm):
You can use a WHERE expression in both a DATA step and SAS procedures,
as well as in a windowing environment, SCL programs, and as a data set
option. A WHERE expression tests the condition before an observation
is read into the PDV. If the condition is true, the observation is
read into the PDV and processed. If the condition is false, the
observation is not read into the PDV, and processing continues with
the next observation. This can yield substantial savings when
observations contain many variables or very long character variables
(up to 32K bytes).
供参考,
我实际上检查了 cpu 时间和 I/O 用法的差异
Keep/Drop
在数据步骤中使用 set 语句而不是 set 语句的语句。
结果是,如果不使用 set 语句,CPU 时间和 I/o 使用量比使用 set 语句的情况少,特别是 I/O 使用量减少 1/2。
并且在设置选项和数据步骤中都出现了 keep/drop 语句,CPU 时间进一步减少,I/O 等于以上观察值。
鉴于 keep / drop 语句也是编译时统计信息,这可能会有所帮助。
where
语句和where
数据集选项的区别在于它们如何生效:前者影响所有没有where
数据集选项的输入数据集,后者仅影响作为选项的数据集(并覆盖 where
语句)。
请参阅 where data set option and the where statement for more information, as well as the Where Expression Processing 概念的文档。特别是,后者在性能上并没有区分两种 where
表达式;它仅指定了它们如何生效的差异。
问题很直接,哪个更快?
考虑到我们在 set 语句中对两个数据集使用数据步骤,并且数据集中具有相同的变量。
根据我所听到和阅读的内容,如果我们使用相同的条件对它们进行子集化,比如 date = "10jan2014"d, the result will be exactly the same using the statement above or the following data set option in the two datasets (where=(date="10jan2014"d))。因为 where 是在任何东西到达 PDV 之前执行的。
对吗?
为了更好地理解问题,我创建了以下代码:
假设我们有这两个数据集。
data people1;
format birth date9.;
input name $ birth :date9.;
datalines;
John 18jan1980
Mary 20feb1980
;
run;
data people2;
format birth date9.;
input name $ birth :date9.;
datalines;
Peter 18mar1980
Judas 18jan1980
;
run;
我想用这两个创建一个新数据集,但只适用于出生日期等于 18jan1980 的人。 现在我可以使用 where 语句或 where 数据集选项。
使用 where(数据集选项):
data everybody1;
set people1 (where=(birth="18jan1980"d))
people2 (where=(birth="18jan1980"d));
run;
使用 where 语句:
data everybody2;
set people1
people2;
where birth="18jan1980"d;
run;
新数据集现在具有完全相同的输出。但是哪个更快?
谢谢。
它们在性能方面是相同的。是的,where 将只允许将匹配结果加载到 PDV。
根据文档 (http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000202951.htm):
The WHERE statement selects observations before they are brought into the program data vector, making it a more efficient programming technique.
上面的引述是在谈论 where
与 if
语句,但它确实确认 where
子句不会将数据带入 PDV。
编辑:感谢 Joe 的 link,我将提供一个更好的文档摘录 (http://support.sas.com/documentation/cdl/en/lrcon/67885/HTML/default/viewer.htm#p04fy20d8il3nfn1ssywe4f25k27.htm):
You can use a WHERE expression in both a DATA step and SAS procedures, as well as in a windowing environment, SCL programs, and as a data set option. A WHERE expression tests the condition before an observation is read into the PDV. If the condition is true, the observation is read into the PDV and processed. If the condition is false, the observation is not read into the PDV, and processing continues with the next observation. This can yield substantial savings when observations contain many variables or very long character variables (up to 32K bytes).
供参考,
我实际上检查了 cpu 时间和 I/O 用法的差异
Keep/Drop
在数据步骤中使用 set 语句而不是 set 语句的语句。
结果是,如果不使用 set 语句,CPU 时间和 I/o 使用量比使用 set 语句的情况少,特别是 I/O 使用量减少 1/2。
并且在设置选项和数据步骤中都出现了 keep/drop 语句,CPU 时间进一步减少,I/O 等于以上观察值。
鉴于 keep / drop 语句也是编译时统计信息,这可能会有所帮助。
where
语句和where
数据集选项的区别在于它们如何生效:前者影响所有没有where
数据集选项的输入数据集,后者仅影响作为选项的数据集(并覆盖 where
语句)。
请参阅 where data set option and the where statement for more information, as well as the Where Expression Processing 概念的文档。特别是,后者在性能上并没有区分两种 where
表达式;它仅指定了它们如何生效的差异。