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;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.