Sequential numbering by unique identifier

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

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

Would greatly appreciate any advice.


Accepted Solutions
Solution
‎06-01-2012 12:02 PM
Respected Advisor
Posts: 3,156

Re: Sequential numbering by unique identifier

Posted in reply to hillcat27

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

View solution in original post


All Replies
Solution
‎06-01-2012 12:02 PM
Respected Advisor
Posts: 3,156

Re: Sequential numbering by unique identifier

Posted in reply to hillcat27

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,324

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

Respected Advisor
Posts: 3,156

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:

new=_n_; instead of new+1;

Regards,

Haikuo

Super Contributor
Posts: 1,636

Re: Sequential numbering by unique identifier

Posted in reply to hillcat27

or

data want;

set have;

by old notsorted;

id+first.old;

proc print;run;

Respected Advisor
Posts: 3,156

Re: Sequential numbering by unique identifier

neat!

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 350 views
  • 4 likes
  • 4 in conversation