SAS Data Step编程 - 解释数据集(obs = 0)

时间:2015-02-27 22:23:54

标签: sas datastep

如果我们在下面的(错误)示例中设置(obs = 0)数据集选项,请解释为什么没有处理数据步骤语句?

data temp;
    x=0;
run;

data wrong;
    set temp(obs=0);
    x=1;
    y=1;
    output;
    y=2;
    output;
run;

data right;
    set temp(obs=1);
    x=1;
    y=1;
    output;
    y=2;
    output;
run;

我通常希望work.wrong和work.right都有相同的输出。

1 个答案:

答案 0 :(得分:4)

数据步骤停止执行的一种方式是SET语句执行并读取文件结束字符(即没有更多记录要读取)。

因此,如果使用(obs = 0)设置数据集,则在执行SET语句时,数据步骤将停止。例如:

122  data _null_ ;
123    put _n_= "I ran" ;
124    set sashelp.class(obs=0) ;
125    put _n_= "I did not run" ;
126  run;

_N_=1 I ran
NOTE: There were 0 observations read from the data set SASHELP.CLASS.

执行第一个PUT语句,但第二个不执行,因为执行SET语句时步骤停止。

当您使用(OBS = 1)设置数据集时,数据步骤将在SECOND迭代中停止:

135  data _null_ ;
136    put _n_= "I ran before SET" ;
137    set sashelp.class(obs=1) ;
138    put _n_= "I ran after SET" ;
139  run;

_N_=1 I ran before SET
_N_=1 I ran after SET
_N_=2 I ran before SET
NOTE: There were 1 observations read from the data set SASHELP.CLASS.