BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ken_oy
Fluorite | Level 6

I want to add a group number to the first dataset (see the second one). Does anyone know how?

I cannot use sort because some categories have the same name. I can only determine the group by the existing data order.

Thanks!

dataset 1:

a

a

b

b

b

c

c

d

d

d

b

b

dataset 2:

a 1

a 1

b 2

b 2

b 2

c 3

c 3

d 4

d 4

d 4

b 5

b 5

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Use the NOTSORTED option of by groups with a retain statement:

data want;

set have;

by id notsorted;

retain group 0;

if first.id then group=group+1;

run;

View solution in original post

5 REPLIES 5
Peter_C
Rhodochrosite | Level 12

set statement referring to a single dataset allows the by statement to use the NOTSORTED option

Peter_C
Rhodochrosite | Level 12

A simple version would be

data ds2 ;

set ds1 ;

by id notsorted ;

group_no + first.id ;

run ;

ballardw
Super User

If the two datasets have the exact same number of records then this may work:

data new;

     set dataset1;

     set dataset2;

run;

Reeza
Super User

Use the NOTSORTED option of by groups with a retain statement:

data want;

set have;

by id notsorted;

retain group 0;

if first.id then group=group+1;

run;

Ken_oy
Fluorite | Level 6

I make my own version too, thank you all for the ideas!

data want;

set test;

retain group 0;

if lag( id ) ne  id then group +1;

run;

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
  • 5 replies
  • 1917 views
  • 9 likes
  • 4 in conversation