Obsidian | Level 7

## 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     .        .        .

while

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

obs vb1   vb2        vb3

1     2    1.28        4

Thank you very much for solving my problem.

3 REPLIES 3
Opal | Level 21

## 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:

http://www2.sas.com/proceedings/sugi31/246-31.pdf

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.

Obsidian | Level 7

## if _N_=1 then set <dataset> question;

Thank you very much for help.

Calcite | Level 5

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

Discussion stats
• 3 replies
• 13161 views
• 6 likes
• 3 in conversation