BookmarkSubscribeRSS Feed
Lulus
Obsidian | Level 7

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

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

Thank you very much for solving my problem. 

3 REPLIES 3
art297
Opal | Level 21

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.

Lulus
Obsidian | Level 7

Thank you very much for help.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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