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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.