开发者

Conditionally retaining variables in SAS

I have th开发者_如何学Gois SAS sample code:

data BEFORE;
    input v1 v2;
datalines;
1 2
;

data AFTER;
    put 'Before IF: ' _ALL_;
    if _N_ = 1 then set BEFORE;
    put 'After  IF : ' _ALL_;
run;

The output is:

BEFORE: v1=. v2=. _ERROR_=0 _N_=1
AFTER : v1=1 v2=2 _ERROR_=0 _N_=1
BEFORE: v1=1 v2=2 _ERROR_=0 _N_=2
AFTER : v1=1 v2=2 _ERROR_=0 _N_=2

And the output file contains:

Obs    v1    v2
1      1     2
2      1     2

I know that the SET will import and RETAIN the BEFORE dataset's variables, but why BEFORE's record gets duplicated?


I ran your sample code, and you omitted a crucial piece of information: This message was in the SAS log: "NOTE: DATA STEP stopped due to looping.". Googling on that message led me to a SAS paper describing the error. It suggested not using an IF statement before the SET statement, but to use the OBS= data set option to restrict the number of observations read.

So you would change the line:

if _N_ = 1 then set BEFORE;

to:

set BEFORE(obs=1);

When I ran your code with this change, the "Before IF:" line still printed twice, and I'm not sure why that is so. But the looping NOTE did not occur, so I believe that is the solution.


The SET is an executable statement, that is, unless being executed, it does not reset variables or load the next observation's data, when the data step is executed. (It sets up or alter PDV when the data step is compiled, though.) Because of the if condition, it is executed only once.

The implicit OUTPUT statement at the bottom outputs an observation per iteration. SAS, monitoring to see if a data step loops infinitely, stops the data step after the second iteration and generates the note.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜