• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / SAS / SAS _n_ – How to Use the Automatic Variable _n_ in a Data Step

SAS _n_ – How to Use the Automatic Variable _n_ in a Data Step

April 22, 2022 Leave a Comment

The SAS automatic variable _n_ represents the number of times the data step has iterated.

As an automatic variable, _n_ is created automatically by SAS when a data step is performed. _n_ is temporary, meaning it is not kept on the dataset after the data step is finished.

When a data step starts, _n_ is initialized to 1. Then, after each iteration through the data step, _n_ is incremented by one.

To see this, let’s say we have the following dataset of 6 observations.

data data;
	input num;
	datalines;
4
16
5
-2
3
-10
;
run;

Let’s put _n_ to the log as we enter a data step with our dataset. Below is the following output of _n_ as we iterate through our dataset.

data data;
   set data;
   put _n_;
run;

/* Output */
 1
 2
 3
 4
 5
 6

As you can see, _n_ starts at 1 and is incremented for each observation.

Adding the Row Number as a Column in SAS with _n_

When working with data in SAS, the ability to easily get information about rows or columns is valuable.

One piece of information which can be valuable to know is the number of the row you are currently operating on.

To get the row number in a SAS data step, you can use the SAS automatic variable _n_.

Let’s say you have the same dataset as above. Let’s get the row number for each of these records.

Below is an example of how you can use _n_ to get the row number in SAS.

data data_with_row_number;
    set data;
    row_num = _n_;
run;

The resulting dataset is as follows.

   num   row_num
1    4         1
2   16         2
3    5         3
4   -2         4
5    3         5
6  -10         6

Useful Applications of _n_ in SAS Data Steps

The SAS _n_ variable can be useful in certain cases.

For example, if you want to perform a certain calculation on a particular record, then you can use an if statement and check if _n_ is equal to the observation number of that record.

data want;
    set have;
    if _n_ = 3 then do;
       /* do stuff here */
    end;
run;

This can be useful if you want to check if you are operating on the first or last observation of a dataset.

data want;
    set have;
    if _n_ = 1 then do;
       /* do stuff related to being on first observation */
    end;
    if _n_ = last then do;
       /* do stuff related to being on last observation */
    end;
run;

Hopefully this article has been useful for you to learn how to use _n_ in your SAS data steps.

Other Articles You'll Also Like:

  • 1.  How to Combine Datasets Vertically in SAS
  • 2.  SAS sum() Function – Find Sum Across Columns in Data Step
  • 3.  SAS floor – Round Down to Floor of Number in a SAS Data Step
  • 4.  Set Multiple Datasets in SAS Data Step
  • 5.  Get Substring from Right in SAS
  • 6.  SAS select when – Evaluating Character Values for Conditional Processing
  • 7.  PROC Format – Create User-Defined Formats for Variables in SAS
  • 8.  Date Format ddmmmyyyy in SAS
  • 9.  SAS trim – Remove All Trailing Blanks from String Variable in Data Step
  • 10.  SAS weekday function – Get Day of Week from Date Variable

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy