I have a dataset that is grouped by a variable 'group':
```
id group
1 a
2 a
3 a
4 b
5 b
6 b
7 c
8 c
9 d
10 d
11 d
12 d
13 d```
My goal is to create a serial number that assigns the same number to obs within the same group:
```
id group number
1 a 1
2 a 1
3 a 1
4 b 2
5 b 2
6 b 2
7 c 3
8 c 3
9 d 4
10 d 4
11 d 4
12 d 4
13 d 4```
how about this
data have;
input id group $;
datalines;
1 a
2 a
3 a
4 b
5 b
6 b
7 c
8 c
9 d
10 d
11 d
12 d
13 d
;
run;
proc sort data=have;
by id group;
run;
data want;
set have;
by id group;
retain number;
if not(group=lag(group)) then number+1;
run;
how about this
data have;
input id group $;
datalines;
1 a
2 a
3 a
4 b
5 b
6 b
7 c
8 c
9 d
10 d
11 d
12 d
13 d
;
run;
proc sort data=have;
by id group;
run;
data want;
set have;
by id group;
retain number;
if not(group=lag(group)) then number+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.