How to Add Row Numbers in SAS (With Examples)

How to Add Row Numbers in SAS (With Examples)

Adding row numbers in SAS is a simple and straightforward process. To do this, you can use the ROW_NUMBER() function, which assigns a unique integer value to each row. This can be used to create a new variable that contains row numbers or to assign the row number to an existing variable. For example, the statement ROW_NUMBER() OVER (ORDER BY variable_name) can be used to assign a row number to each observation in the data sorted by the specified variable.


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 sort data=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 print data=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 sort data=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.

The following tutorials explain how to perform other common tasks in SAS:

Cite this article

stats writer (2025). How to Add Row Numbers in SAS (With Examples). PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-to-add-row-numbers-in-sas-with-examples/

stats writer. "How to Add Row Numbers in SAS (With Examples)." PSYCHOLOGICAL SCALES, 1 Dec. 2025, https://scales.arabpsychology.com/stats/how-to-add-row-numbers-in-sas-with-examples/.

stats writer. "How to Add Row Numbers in SAS (With Examples)." PSYCHOLOGICAL SCALES, 2025. https://scales.arabpsychology.com/stats/how-to-add-row-numbers-in-sas-with-examples/.

stats writer (2025) 'How to Add Row Numbers in SAS (With Examples)', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-to-add-row-numbers-in-sas-with-examples/.

[1] stats writer, "How to Add Row Numbers in SAS (With Examples)," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, December, 2025.

stats writer. How to Add Row Numbers in SAS (With Examples). PSYCHOLOGICAL SCALES. 2025;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top