Table of Contents
Row numbers in SAS refer to the sequential numbering of rows in a dataset. This allows for easier identification and organization of data. There are several ways to add row numbers in SAS, including using the DATA step, PROC SQL, and PROC SORT.
In the DATA step, the automatic variable _N_ can be used to generate row numbers by using the statement _N_ + 1. This will add a new column to the dataset containing the row numbers.
In PROC SQL, the ROW_NUMBER() function can be used to add row numbers. This function assigns a sequential number to each row in the dataset.
Another method is to use the ID statement in PROC SORT, which creates a new column containing the row numbers based on the order of the data.
By using these methods, row numbers can be easily added to datasets in SAS, providing a useful tool for data analysis and manipulation.
Add Row Numbers in SAS (With Examples)
You can use the following methods to add row numbers to a dataset in SAS:
Method 1: Add Row Number
data my_data2;
row_number = _N_;
set my_data1;
run;Method 2: Add Row Number by Group
/*sort original dataset by var1*/
proc sortdata=my_data1;
by var1;
run;
/*create new dataset that shows row number by var1*/
data my_data2;
set my_data1;
by var1;
if first.var1 then row_number=0;
row_number+1;
run;The following examples show how to use each method with the following dataset in SAS:
/*create dataset*/ data my_data1; input team $ points; datalines; Mavs 22 Mavs 40 Rockets 41 Rockets 29 Rockets 30 Spurs 18 Spurs 22 Spurs 27 Warriors 13 Warriors 19 ; run; /*view dataset*/ proc printdata=my_data1;

Example 1: Add Row Number
The following code shows how to add a new column called row_number that contains the row number for each observation:
/*create new dataset with column for row numbers*/
data my_data2;
row_number = _N_;
set my_data1;
run;

Notice that a new column called row_number has been added that contains the row number for each observation in the dataset.
Example 2: Add Row Number by Group
The following code shows how to add a row number by group:
/*sort original dataset by team*/
proc sortdata=my_data1;
by var1;
run;
/*create new dataset that shows row number by team*/
data my_data2;
set my_data1;
by var1;
if first.var1 then row_number=0;
row_number+1;
run;
Notice that the row numbers start over for each new team.
Additional Resources
The following tutorials explain how to perform other common tasks in SAS:
Cite this article
stats writer (2024). “What are some examples of how to add row numbers in SAS?”. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/what-are-some-examples-of-how-to-add-row-numbers-in-sas/
stats writer. "“What are some examples of how to add row numbers in SAS?”." PSYCHOLOGICAL SCALES, 1 Jul. 2024, https://scales.arabpsychology.com/stats/what-are-some-examples-of-how-to-add-row-numbers-in-sas/.
stats writer. "“What are some examples of how to add row numbers in SAS?”." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/what-are-some-examples-of-how-to-add-row-numbers-in-sas/.
stats writer (2024) '“What are some examples of how to add row numbers in SAS?”', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/what-are-some-examples-of-how-to-add-row-numbers-in-sas/.
[1] stats writer, "“What are some examples of how to add row numbers in SAS?”," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.
stats writer. “What are some examples of how to add row numbers in SAS?”. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
