## Assign unique id

Solved
Occasional Contributor
Posts: 15

# Assign unique id

Hi all,

I have a dataset in which I need to assign a unique id by two variables.

Below is what I have:

id      reg_seq

1       1

1       1

1       2

1       3

2       1

2       2

2       2

This is what I want:

id      reg_seq       seq

1       1                  1

1       1                  1

1       2                  2

1       3                  3

2       1                  4

2       2                  5

2       2                  5

Thanks!

Accepted Solutions
Solution
3 weeks ago
Super User
Posts: 6,753

## Re: Assign unique id

Assuming your data set is sorted so that the BY statement works:

data want;

set have;

by id reg_seq;

if first.reg_seq then seq + 1;

run;

All Replies
Solution
3 weeks ago
Super User
Posts: 6,753

## Re: Assign unique id

Assuming your data set is sorted so that the BY statement works:

data want;

set have;

by id reg_seq;

if first.reg_seq then seq + 1;

run;

Occasional Contributor
Posts: 15

Thank you, both!

Contributor
Posts: 42

## Re: Assign unique id

Hi, heretolearn:

If you know First (or Last) By Variables & Retain Statement, you can get your objective submitting this code.

``````data test;
input id reg_seq;
datalines;
1 1
1 1
1 2
1 3
2 1
2 2
2 2
;
run;
proc sort data=test out=test_1;
by id reg_seq;
run;
data test_2;
set test_1;
retain seq;
by id reg_seq;
if first.reg_seq eq 1 then byflg=1;
if _n_ eq 1 then seq=0;
if byflg ne . then seq+byflg;
run;``````
☑ This topic is solved.