BookmarkSubscribeRSS Feed
koya
Fluorite | Level 6

data:

accid

====

abc

abc

def

abc

xyz

def

desired output:

accid       uniqid

====      =====

abc            1

abc             1

def              2

abc            1

xyz            3

def             2

5 REPLIES 5
RamKumar
Fluorite | Level 6

Try this.

data have;

input accid $;

datalines;

abc

abc

def

abc

xyz

def

run;

proc sort data=have;

by accid;

run;

data want (rename=count=uniqid);

set have;

by accid;

if first.accid then count+1;

else count=count;

run;

data_null__
Jade | Level 19

Does

else count=count;

do anything?

LinusH
Tourmaline | Level 20

Just by looking in your example, it looks like a surrogate key.

What's the application/requirement? This will just create 1-1 relationships.

If you want to work with lots of data and giving them surrogate keys (and more), look an ETL tool such as DI Studio.

Data never sleeps
data_null__
Jade | Level 19

If you don't want to sort you could do something like this.  If you have multiple keys this will also work by creating a compound index and adding variables to the CLASS statement.

data have;
   input accid $;
   cards;
abc
abc
def
abc
xyz
def
;;;;
   run;

proc summary data=have nway;
  
class accid;
   output out=uniqid(drop=_type_ _freq_ rename=(_level_=uniqid) index=(accid)) / levels;
  
run;
proc print;
  
run;
data withid;
   set have;
   set uniqid key=accid/unique;
  
run;
proc print;
  
run;

3-4-2015 7-17-51 AM.png
koya
Fluorite | Level 6

Thank you all, that helps!

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

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 5 replies
  • 4673 views
  • 6 likes
  • 4 in conversation