BookmarkSubscribeRSS Feed
gonzy31
Calcite | Level 5

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

3 REPLIES 3
Astounding
PROC Star

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.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 3592 views
  • 0 likes
  • 3 in conversation