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

 

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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