BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
james_peacock
Fluorite | Level 6

Hi,

 

I'm new to SAS and I can't seem to find out what the _N_ represents in the following bit of code:

 

uniform(_N_)

 

It doesn't seem to have been defined anywhere within the code.

 

Thanks

J

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

In a datastep, and with some procedures and such like, hidden variables get created at certain points.  All documented in the user manual.  _n_ is one of those variables and represent logic position of row in the dataset, so first row is 1, second 2 etc.

 

View solution in original post

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

In a datastep, and with some procedures and such like, hidden variables get created at certain points.  All documented in the user manual.  _n_ is one of those variables and represent logic position of row in the dataset, so first row is 1, second 2 etc.

 

james_peacock
Fluorite | Level 6
Thank you very much!!
FreelanceReinh
Jade | Level 19

Hi @james_peacock and welcome to the SAS Support Communities!

 

In many DATA steps the value of automatic variable _N_ coincides with some observation number. But there are many exceptions to this rule of thumb because in fact _N_ doesn't enumerate observations, but iterations of the DATA step (see Overview of DATA Step Processing and for automatic variables in general: Automatic Variables).

 

Here are some examples (note that PUT statements write to the SAS log by default):

/* Read 19 observations: _N_ and observation number coincide */

data _null_;
set sashelp.class;
put _n_=;
run;

/* Make 20th DATA step iteration visible */

data _null_;
put _n_=;
set sashelp.class;
run;

/* Read and write 19 obs. within a single iteration of the DATA step: _N_=1 */

data test;
do until(0); /* (no infinite loop thanks to SET statement) */
  set sashelp.class;
  output;
  put _n_=;
end;
run;

/* Create dataset from data lines: _N_=1, 2, 3 */

data a;
input x;
put _n_=;
cards;
10
20
30
;

/* Create same dataset from same data lines, but within
   one iteration of the DATA step: _N_=1 */

data a;
input x;
output;
input x;
output;
input x;
output;
put _n_=;
cards;
10
20
30
;

/* No datasets involved, still _N_=1 */

data _null_;
put _n_=;
run;

/* _N_ can be "mis"used for other purposes (here: as an index variable 
   of a DO loop that is dropped automatically). It's not read-only. */

data squares;
do _n_=1 to 5;
  square=_n_**2;
  output;
end;
run;

If your example refers to the UNIFORM function, using uniform(_N_) is possibly questionable or misleading (depending on the context) not only because this function is deprecated, but because multiple calls of the function with varying values of _N_ in the same DATA step always use the same seed value.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 1083 views
  • 1 like
  • 3 in conversation