BookmarkSubscribeRSS Feed
srikanthyadav44
Quartz | Level 8

i have a data of 5000 companies which are repeated for multiple number of times. 

I want to code each company as comp1, comp2, comp3 etc., 

 

the sample of the file is as follows 

company
a
a
a
a
a
b
b
b
b
b
c
c
c
c
c

 

the desired output should be like this 

companycode
acomp1
acomp1
acomp1
acomp1
acomp1
bcomp2
bcomp2
bcomp2
bcomp2
bcomp2
ccomp3
ccomp3
ccomp3
ccomp3
ccomp3

 

thanks in advance

3 REPLIES 3
Kurt_Bremser
Super User

Use a retained variable:

data want;
set have;
by company;
retain comp_count 0;
if first.company then comp_count + 1;
code = cats('comp',comp_count);
drop comp_count;
run;
novinosrin
Tourmaline | Level 20

data have;
input company $;
cards;
a
a
a
a
a
b
b
b
b
b
c
c
c
c
c
;

data want;
do until(last.company);
set have;
by company;
length code $10;
code=cats('COMP',_n_);
output;
end;
run;

Astounding
PROC Star

A good idea, but it can be done with a slightly quicker program:

 

data want;
length code $ 10;
code = cats('comp', _n_);
do until (last.company);
   set have;
   by company;
   output;
end;
run;

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!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 732 views
  • 7 likes
  • 4 in conversation