SAS do 循环和 set 语句
SAS do-Loops and set statements
为什么这个宏有效? (确实如此)循环能够开始,尽管 nrows
变量是在循环内的 set
语句中定义的。 SAS 在开始循环之前是否读取 set 语句?我在哪里可以找到关于这个问题的文档(循环内的哪些语句,如果有的话,在循环开始之前执行)?
%macro get_last_n_rows(n, existing, new);
data &new.;
do _i_ = 1 + nrows - &n. to nrows;
set &existing. point = _i_ nobs = nrows;
output;
end;
stop;
run;
%mend get_last_n_rows;
对您的问题的简短回答是:是的,SAS 会在循环执行之前读取可用的行数。事实上,SAS 会在数据步骤执行之前读取可用的行数;它是在数据步骤编译时确定的。例如,请参阅 this paper and this paper,以及其他许多内容。
At compilation time, SAS reads the descriptor portion of each data set and assigns the value of the NOBS= variable automatically. Thus, you can refer to the NOBS= variable before the SET statement. The variable is available in the DATA step but is not added to any output data set.
注意这与 do
循环无关;这对于整个数据步骤都是如此(当然,它本身就是一个大循环)。
为什么这个宏有效? (确实如此)循环能够开始,尽管 nrows
变量是在循环内的 set
语句中定义的。 SAS 在开始循环之前是否读取 set 语句?我在哪里可以找到关于这个问题的文档(循环内的哪些语句,如果有的话,在循环开始之前执行)?
%macro get_last_n_rows(n, existing, new);
data &new.;
do _i_ = 1 + nrows - &n. to nrows;
set &existing. point = _i_ nobs = nrows;
output;
end;
stop;
run;
%mend get_last_n_rows;
对您的问题的简短回答是:是的,SAS 会在循环执行之前读取可用的行数。事实上,SAS 会在数据步骤执行之前读取可用的行数;它是在数据步骤编译时确定的。例如,请参阅 this paper and this paper,以及其他许多内容。
At compilation time, SAS reads the descriptor portion of each data set and assigns the value of the NOBS= variable automatically. Thus, you can refer to the NOBS= variable before the SET statement. The variable is available in the DATA step but is not added to any output data set.
注意这与 do
循环无关;这对于整个数据步骤都是如此(当然,它本身就是一个大循环)。