How to 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 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;

The resulting table shows the row number by each team.

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:

How to Normalize Data in SAS
How to Rename Variables in SAS
How to Remove Duplicates in SAS
How to Replace Missing Values with Zero in SAS

Leave a Reply

Your email address will not be published.