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

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

```

1 ACCEPTED SOLUTION

Accepted Solutions
japelin
Rhodochrosite | Level 12

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;

 

View solution in original post

2 REPLIES 2
japelin
Rhodochrosite | Level 12

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;

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 2 replies
  • 1056 views
  • 0 likes
  • 2 in conversation