Grouping by Unique combination of two variables

Reply
Contributor
Posts: 66

Grouping by Unique combination of two variables

Data have;

input    Name   $    Count;

datalines;

a 2

a 2

a 3

b 4

b 4

c 3

c 1

d 2

d 2

d 2

d 2

;

I want to create a data set want with a new variable  group so that this variable takes a uniqe value for each unique combination of Name and count.

Thus the data set want should look like

Want

Name    Count  Group

a           2          1

a           2          1

a           3          2

b           4          3

b           4          3

c           1          4

c           3          5

d           2          6

d           2          6

d           2          6

d           2          6

;

Thank you for your attention!

Super User
Posts: 19,815

Re: Grouping by Unique combination of two variables

You can use your BY groups and a first.Count, because when that changes is when you want your count to increment.

Data want;

set have;

by name count;

retain group 0;

if first.count then group+1;

run;

EDIT: to reflect LinLin's suggestion Smiley Happy

Super Contributor
Posts: 1,636

Re: Grouping by Unique combination of two variables

should be

Data want;

set have;

by name count;

retain group 0;

if first.count then group+1;

run;

or

Data want;

set have;

by name count;

if first.count then group+1;

run;

Super User
Posts: 19,815

Re: Grouping by Unique combination of two variables

Smiley Happy thanks. That's why you should always test

I'll say that I'm leaving something for the OP to do Smiley Wink

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