DATA Step, Macro, Functions and more

Assign Group ID for Same Values in Increasing Order

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

Assign Group ID for Same Values in Increasing Order

I would like to assign group id for same values,

like showed below, if I have code_name, how to get group id number?

 

code_name    group_id_number

aa                     1

aa                     1

aa                     1

bb                    2

cc                     3

cc                     3

dd                    4

ee                    5

ff                      6

gg                    7

gg                    7

.                               .

.                               .

.                               .


Accepted Solutions
Solution
‎05-30-2017 02:49 PM
Super User
Posts: 7,863

Re: Assign Group ID for Same Values in Increasing Order

[ Edited ]

Although a retain statement is not needed when you use the s_id + 1 syntax, I like to use it for initializing:

data test;
set test2 (keep = codename);
by codename;
retain s_id 0;
if first.codename then s_id + 1;
run;

Edit: fixed a typo in the by statement (wrong underline)

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Super User
Posts: 5,438

Re: Assign Group ID for Same Values in Increasing Order

Use a data step with BY processing.
Then
if first.codename then group_id_number+1;
You need to initialize group_id_number to 0 (hint: _n_=0)
Data never sleeps
Occasional Contributor
Posts: 7

Re: Assign Group ID for Same Values in Increasing Order

Here is my code:

 

data test;
set test2 (keep = codename);
by code_name;

s_id = _n_ = 0;

if first.codename then s_id + 1;
run;

proc print data = test (obs = 100);
var codename s_id;
run;

 

And the out are something like this:

It only assign "1" to the very first codename and the duplicates will just have 0 assigned

 

aa                     1

aa                     0

aa                     0

bb                    1

cc                     1

cc                     0

dd                    1

ee                    1

ff                      1

gg                    1

gg                    0

.                               .

.                               .

.                               .

 

Solution
‎05-30-2017 02:49 PM
Super User
Posts: 7,863

Re: Assign Group ID for Same Values in Increasing Order

[ Edited ]

Although a retain statement is not needed when you use the s_id + 1 syntax, I like to use it for initializing:

data test;
set test2 (keep = codename);
by codename;
retain s_id 0;
if first.codename then s_id + 1;
run;

Edit: fixed a typo in the by statement (wrong underline)

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 7

Re: Assign Group ID for Same Values in Increasing Order

Posted in reply to KurtBremser

Thanks!!

 

It worked perfectly!Smiley Very Happy

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 109 views
  • 1 like
  • 3 in conversation