/***************************************************************************************************************** SAS file name: Variable_N_.sas File location: __________________________________________________________________________________________________________________ Purpose: To demonstrate how to think of the automatic variable _N_ in SAS Author: Peter Clemmensen Creation Date: 05/08/2019 This program supports the blog post "Understand the Automatic _N_ Variable in SAS" on SASnrd.com *****************************************************************************************************************/ /* Example data set */ data have; input x @@; datalines; 1 2 3 ; /* Simple use of the _N_ variable: Put number of data step iterations in the log */ data test; set have; put _N_=; run; /* _N_ is initially set to 1 */ data test2; put _N_=; set have; put _N_= //; run; /* _N_ increments each time the data step loops past data Data Statement */ data test3; set have; put _N_=; _N_=0; put _N_= //; run; /* In the below DoW Loop, _N_ will remain 1 */ data test4; do until (lr); set have end=lr; put _N_=; end; run; /* We can force the increment of _N_ in the following way */ data test5; do _N_=1 by 1 until (lr); set have end=lr; put _N_=; end; run;