DATA Step, Macro, Functions and more

if _N_=1 then set <dataset> question;

Posts: 46

if _N_=1 then set <dataset> question;

Hello all, I've been stocked with this problem.  When I run a statement that


data five;

if _N_=1 then set <data1>; "

The output shows me 2 records.  looks like this in the following,

obs vb1   vb2        vb3

1     2    1.28        4

2     2    1.28        4

I am so confused that if the _N_=1 indicates the first observation in a data set, shouldn't I have only one observation in the output only?

A extended quesion,

since I wanted to try this problem out, I put if _N_=2 if _N_<=100 to replace my statement above,

in _N_=2 case, it shows only the first observation with the variable names from data1 but appear missing values for all the numbers.

obs vb1   vb2     vb3

1     .        .        .


in _N_<=100 case, the output shows one observation with correct number copied;

obs vb1   vb2        vb3

1     2    1.28        4

Both of the outputs are unexpected.  Please help me with the SAS logic. 

Thank you very much for solving my problem. 

Posts: 8,145

if _N_=1 then set <dataset> question;

A nice explanation of how the datastep works can be found in the following paper by Ian Whitlock:

In that paper, Ian give a much better explanation than I could in this brief response.  A simplified explanation for the "if _N_ eq 1" case, is that the datastep has to start the second iteration in order to know that it is no longer the first iteration.  You get the second copy of the first record because the value from the first record are automatically retained.and, since it is in its second iteration, _n_ is incremented to 2.

In the _N_ <= 100 case, since you really only have one record, SAS reaches the end of file normally and simply exits normally.

In the _N_ eq 2 case, the variable names and formats are read before the first iteration begins.  However, the code immediately exits because the implied loop was interrupted.

Posts: 46

if _N_=1 then set <dataset> question;

Thank you very much for help.

Senior User
Posts: 1

Re: if _N_=1 then set <dataset> question;

Ask a Question
Discussion stats
  • 3 replies
  • 3 in conversation