## Sequential numbering by unique identifier

Solved
Occasional Contributor
Posts: 10

# Sequential numbering by unique identifier

Greetings,

I have a large dataset where I need a new column that identifies my subject character variable as a unique numeric ID, without using hundreds of 'if/then' statements.

Example:

HAVE     NEED

A1          1

A1          1

A1          1

A1          1

A4          2

A4          2

A4          2

A4          2

A4          2

A4          2

A8          3

A8          3

Accepted Solutions
Solution
‎06-01-2012 12:02 PM
Posts: 3,167

## Re: Sequential numbering by unique identifier

DOW can do the trick:

data HAVE;

input old\$;

cards;

A1

A1

A1

A1

A4

A4

A4

A4

A4

A4

A8

A8

;

data want;

new+1;

do until (last.old);

set have;

by old notsorted;

output;

end;

run;

proc print;run;

Haikuo

All Replies
Solution
‎06-01-2012 12:02 PM
Posts: 3,167

## Re: Sequential numbering by unique identifier

DOW can do the trick:

data HAVE;

input old\$;

cards;

A1

A1

A1

A1

A4

A4

A4

A4

A4

A4

A8

A8

;

data want;

new+1;

do until (last.old);

set have;

by old notsorted;

output;

end;

run;

proc print;run;

Haikuo

PROC Star
Posts: 1,460

## Re: Sequential numbering by unique identifier

DOW is good to know, but I don't think it's needed here.  I think by-group processing alone can do it:

```data want;
set have;
by old notsorted;
if first.old then id++1;
run;
```
Occasional Contributor
Posts: 10

## Re: Sequential numbering by unique identifier

Both work just fine.  many thanks for your prompt guidance

Posts: 3,167

## Re: Sequential numbering by unique identifier

Couldn't agree more, DOW here is using by group processing anyway.

My intention was to use the internal counter of DOW, but I forgot about it somehow the time I put up the answer. It is supposed to be:

Regards,

Haikuo

Super Contributor
Posts: 1,636

## Re: Sequential numbering by unique identifier

or

data want;

set have;

by old notsorted;

id+first.old;

proc print;run;

Posts: 3,167

## Re: Sequential numbering by unique identifier

neat!

🔒 This topic is solved and locked.