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.

## Leave a Reply