Creating a new unique ID variable from two variables in merged data with repetition of one variable

Reply
Occasional Contributor
Posts: 9

Creating a new unique ID variable from two variables in merged data with repetition of one variable

Hi all,

 

Would love some help with this, as I think I am doing something wrong here in SAS.  I merged multiple years of data, but I wanted to create a unique ID variable ("ID") to identify each unique physician ("phycode").  For each individual year, the "phycode" is unique for every physician.  However, the "phycode" is repeated in subsequent years for other physicians so both "phycode" and "year" are necessary to create a new unique ID in the dataset.

 

Here is what my data looks like now:

 

Phycode        Year

1                     2004

1                     2005

2                     2004

2                     2005

3                     2004

4                     2004

 

I want my output to produce a table like this in recognition that repeat phycodes in different years represent unique physicians (ID):

 

Phycode        Year              ID

1                     2004             1

1                     2005             2

2                     2004             3

2                     2005             4

3                     2004             5

4                     2004             6

 

 

Here is what I tried:

proc sort data=IN.NAMCS0514;

by phycode year;
run; 
 
data=UniqueID;
set NSS0514;
by phycode year;
if  first.phycode then ID=1;
ID +1;
run;

 

Thanks in advance for your help

Super User
Posts: 5,088

Re: Creating a new unique ID variable from two variables in merged data with repetition of one varia

Well you could replace the last two statements with:

 

if first.year then ID + 1;

 

Alternatively, you don't really need an ID variable,  You could just use the combination of PHYCODE and YEAR to identify a physician.

PROC Star
Posts: 1,564

Re: Creating a new unique ID variable from two variables in merged data with repetition of one varia

[ Edited ]

Your output matches:

data UniqueID;
set NSS0514; 
ID +1;
run;
Occasional Contributor
Posts: 9

Re: Creating a new unique ID variable from two variables in merged data with repetition of one varia

Thank you both! That worked perfectly.

Ask a Question
Discussion stats
  • 3 replies
  • 107 views
  • 0 likes
  • 3 in conversation