Conditionally persistent variables in SAS

I have a SAS example 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;

Conclusion:

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 SET will import and RETAIN before the data set parameters, but why is the BEFORE record duplicated?

+3
source share
2 answers

I ran your sample code and you omitted important information: this message was in the SAS log: "NOTE: DATA STATION was stopped due to cyclization." As a result of this post, I provided a SAS document describing the error . He suggested not using the IF statement before the SET statement, but using the OBS = dataset parameter to limit the number of observations read.

So you should change the line:

if _N_ = 1 then set BEFORE;

at

set BEFORE(obs=1);

, "Before IF:" , , . , , .

+2

SET , , , reset , . ( PDV, .) - if .

OUTPUT . SAS, , , , .

0

Source: https://habr.com/ru/post/1788176/


All Articles