BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SAS_inquisitive
Lapis Lazuli | Level 10
data test;
  do i = 1 to 10;
  N=_n_;
  output;
  end;
run;

data test;
  do i = 1 to 10;
  output;
  N=_n_;
  end;
run;

I am comparing these programs. The second program has first missing N.  My impression is  that do loop is executing on the first implicit loop of the data test.  That is why N is 1 for all observations. I am not sure why second  progam creates first missing N.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
You output the observation to the data set BEFORE you assign the value to N so it's missing for the first iteration.

View solution in original post

7 REPLIES 7
Reeza
Super User
You output the observation to the data set BEFORE you assign the value to N so it's missing for the first iteration.

ballardw
Super User

From the documentation: _N_ is initially set to 1. Each time the DATA step loops past the DATA statement, the variable _N_ increments by 1. The value of _N_ represents the number of times the DATA step has iterated.

 

Since you do not have a set statment bringing in data then the Data step is only interated once so _n_ is 1.

The rest is timing.

SAS_inquisitive
Lapis Lazuli | Level 10

Thanks, Reeza and ballardw.  Is there any exmple of  explicit loop and implicit loop working  together?  I have heard about dow-loop, I wonder if it is example of interplay of inner and outer loop.

SAS_inquisitive
Lapis Lazuli | Level 10
I think I came up with an example.

data test;
set sashelp.class;
do i=1 to 5;
N=_n_;
output;

end;
run;

I created this dow-loop example by putting set inside do group which does not quite make sense.

data test;
do i=1 to 5;
set sashelp.class;
output
end;
run;
ballardw
Super User

Try this and be prepared to be almost amazed:



data test;
   do i=1 to 5;
      set sashelp.class;
      n=_n_;
      output;
   end;
run;
SAS_inquisitive
Lapis Lazuli | Level 10

looks like explicit do loop is taking control of implicit (inner loop). I thought _n_ should be 1 to 19.

ballardw
Super User

It shows that SET is an executeable statment that can be conditionally executed, for instead use one data set and condtionally bring in values from another data set, interleave two or more datasets, use a variable in one data set to controll how many records are brought from another while repeating the values from the first set.

 

All kinds of ways to mix things up. Also the reason why you'll find cautions about using _n_ as counter for number of records. _n_ will work fine for single datasets or a single Set with multiple data sets. But as soon as you have 2 or more Set statements the reliability of _n_ to tell you the number of records goes way down.

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
  • 7 replies
  • 1403 views
  • 1 like
  • 3 in conversation