BookmarkSubscribeRSS Feed
helloagainoh2
Calcite | Level 5

Lets say I have a data set consisting of names and I would like to assign them a unique id. 

 

Tom

Mary

Jill

Tom

 

How can I make sure tom gets the same id even though he shows up twice in the data set?

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

You could do this

 

data have;
input name $;
datalines;
Tom  
Mary 
Jill 
Tom  
;

data want;
   if _N_ = 1 then do;
      dcl hash h();
      h.definekey('name');
      h.definedata('id');
      h.definedone();
   end;

   set have;

   if h.find() ne 0 then do;
      id + 1; 
      h.add();
   end;
run;

 

Result:

 

Obs name id 
1   Tom  1 
2   Mary 2 
3   Jill 3 
4   Tom  1 
PeterClemmensen
Tourmaline | Level 20

Also, if you don't care that the ID's are increasing, starting from one, you could do something like this

 

data have;
input name $;
datalines;
Tom  
Mary 
Jill 
Tom  
;

data want;
   set have;
   id = input(md5(name), pib3.);
run;
Reeza
Super User

Create a lookup table and make a format that can be applied to all tables.

 

Example is here:

https://gist.github.com/statgeek/fd94b0b6e78815430c1340e8c19f8644

 

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1027 views
  • 3 likes
  • 3 in conversation