【问题标题】:Understanding an implicit loop in SAS data step了解 SAS 数据步骤中的隐式循环
【发布时间】:2015-11-14 05:37:53
【问题描述】:

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

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

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

我没有在程序中指定任何循环,但是如何为每个数据行记录 PUT 语句?

隐式循环在哪里?

【问题讨论】:

    标签: sas


    【解决方案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
    ;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多