• 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
  • VBA
  • 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 substr() Function – Get Substring from Character Variable
  • 2.  PROC Format – Create User-Defined Formats for Variables in SAS
  • 3.  SAS Remove Labels from Dataset with PROC DATASETS
  • 4.  =">SAS Greater Than or Equal to with GE or >=
  • 5.  SAS intnx – Add or Subtract Time from Date Variables in SAS Data Step
  • 6.  SAS floor – Round Down to Floor of Number in a SAS Data Step
  • 7.  SAS Proper Case – Capitalize First Letter of Word with propcase Function
  • 8.  Multiple Condition If Statements in SAS Macro Language
  • 9.  SAS left() Function – Left Align Character Variables in Data Step
  • 10.  SAS tranwrd() Function – Replace Characters in Strings in SAS Data Step

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy