了解SAS数据步骤中的隐式循环

时间:2015-11-13 19:03:57

标签: sas

我正在尝试这个示例SAS程序来理解DATA步骤:

DATA WITHOUT_1;
PUT "Before the INPUT statement:  " _ALL_;

INPUT X @@;
PUT "After the INPUT statement:   " _ALL_ /;
DATALINES;
1 2 . 3
;

我没有在程序中指定任何循环,但是为每条数据线记录了PUT语句怎么样?

隐式循环在哪里?

1 个答案:

答案 0 :(得分:1)

DATA步骤本身是一个隐式循环。

请注意,运行代码时,日志列表包含循环计数器_N_

您的步骤重复5次,因为在第5次迭代中,没有更多数据需要读取并停止:

1    DATA WITHOUT_1;
2    PUT "Before the INPUT statement:  " _ALL_;
3
4    INPUT X @@;
5    PUT "After the INPUT statement:   " _ALL_ /;
6    DATALINES;

Before the INPUT statement:  X=. _ERROR_=0 _N_=1
After the INPUT statement:   X=1 _ERROR_=0 _N_=1

Before the INPUT statement:  X=. _ERROR_=0 _N_=2
After the INPUT statement:   X=2 _ERROR_=0 _N_=2

Before the INPUT statement:  X=. _ERROR_=0 _N_=3
After the INPUT statement:   X=. _ERROR_=0 _N_=3

Before the INPUT statement:  X=. _ERROR_=0 _N_=4
After the INPUT statement:   X=3 _ERROR_=0 _N_=4

Before the INPUT statement:  X=. _ERROR_=0 _N_=5
NOTE: SAS went to a new line when INPUT statement reached past the end of a line.
NOTE: The data set WORK.WITHOUT_1 has 4 observations and 1 variables.

8    ;

注意,用显式循环替换隐式循环也是可能的(有时是有用的),例如:

DATA WITHOUT_1;
  do i=1 to 4;
    PUT "Before the INPUT statement:  " _ALL_;
    INPUT X @@;
    PUT "After the INPUT statement:   " _ALL_ /;
  end;
  stop;
DATALINES;
1 2 . 3
;