• 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 / Get the Row Number in a SAS Data Step with _n_

Get the Row Number in a SAS Data Step with _n_

April 22, 2022 Leave a Comment

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

data want;
    set have;
    row_number = _n_;
run;

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 following dataset.

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

There are six observations in this dataset. 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

Getting the Row Number by Group in a SAS Data Step

If you are working with a dataset and using a by statement, _n_ won’t work for you.

In this case, you will need to create your own row number variable.

Let’s say we have the following dataset.

data data;
	input group_id $2.;
	datalines;
A
B
B
C
A
B
;
run;

First, we need to sort out data with proc sort and then we can use a by statement and get the row number in the following way.

proc sort data=data;
by group_id;
run;

data data_with_row_numbers;
    set data;
    by group_id;
    if first.group_id then row_num = 0;
    row_num + 1;
run;

The resulting dataset is shown below.


   group_id row_num
1         A       1
2         A       2
3         B       1
4         B       2
5         B       3
6         C       1

Hopefully this article has been useful for you to learn how you can get row numbers in SAS.

Other Articles You'll Also Like:

  • 1.  SAS include – Execute Code from Other Files in SAS with %include
  • 2.  SAS Remove Labels from Dataset with PROC DATASETS
  • 3.  SAS Less Than or Equal to with LE or <=
  • 4.  SAS call symput – Create Macro Variables in Data Step
  • 5.  nodup vs nodupkey PROC SORT Options in SAS
  • 6.  SAS right() Function – Right Align Character Variables in Data Step
  • 7.  SAS let – Create User-Defined Macro Variables in Your SAS Code
  • 8.  SAS Power Function – Exponentiate Numbers with ** in a Data Step
  • 9.  Round Number to 2 Decimal Places in SAS
  • 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