Solved
Contributor
Posts: 39

Getting a counter for Identification Number

I am sure this is easy, but I have been trying to figure it out and can't. I have a list of IDs, where some of them are duplicates. What I want is to number all of the ids so that they have a number next to them identifying which observation is which based on some sort criteria.

Data&colon;

ID       Numeric_Var

1                 5

2                28

2                42

3                  2

3                  9

3                 74

And I want a new variable so that my data looks like this:

ID       Numeric_Var         Count

1                 5                        1

2                28                       1

2                42                       2

3                  2                       1

3                  9                       2

3                 74                      3

Any help would be great.

Thanks

Accepted Solutions
Solution
‎04-07-2016 01:50 PM
Super User
Posts: 13,534

Re: Getting a counter for Identification Number

If you don't mind sorting the data (assuming it is not currently in order of the ID variable);

proc sort data=have; by id; run;

data want;

set have;

by id;

retain count; /* I would probably use something more like sequencenumber ...*/

if first.id then count=1;

else count+1;

run;

All Replies
Solution
‎04-07-2016 01:50 PM
Super User
Posts: 13,534

Re: Getting a counter for Identification Number

If you don't mind sorting the data (assuming it is not currently in order of the ID variable);

proc sort data=have; by id; run;

data want;

set have;

by id;

retain count; /* I would probably use something more like sequencenumber ...*/

if first.id then count=1;

else count+1;

run;

Contributor
Posts: 39

Re: Getting a counter for Identification Number

Perfect great thanks!

🔒 This topic is solved and locked.