jaamor
jaamor

Reputation: 317

SAS Data Step programming - explain dataset(obs=0)

Could you please explain why none the data step statements are processed if we set the (obs=0) data set option in the (wrong) example below?

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;

I would normally expect that both work.wrong and work.right have the same output.

Upvotes: 0

Views: 1755

Answers (1)

Quentin
Quentin

Reputation: 6378

One of the ways a data step stops executing is when a SET statement executes and reads an end-of-file character (i.e. there are no more records to read).

So if you SET a dataset with (obs=0), when the SET statement executes, the data step stops. For example:

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.

The first PUT statement executes, but the second does not, because the step stopped when the SET statement executed.

When you SET a dataset with (OBS=1), the data step stops on the SECOND iteration:

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.

Upvotes: 4

Related Questions