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
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;
set statement referring to a single dataset allows the by statement to use the NOTSORTED option
A simple version would be
data ds2 ;
set ds1 ;
by id notsorted ;
group_no + first.id ;
run ;
If the two datasets have the exact same number of records then this may work:
data new;
set dataset1;
set dataset2;
run;
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;
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;
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!
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.
Ready to level-up your skills? Choose your own adventure.