BookmarkSubscribeRSS Feed
heretolearn
Obsidian | Level 7

Hello all,

 

I have a question about how to assign a sequential number by two variables.

 

Below is what I have:

 

Id         reg

1          1

1          1

1          3

1          3

1          3

1          4

2          2

2          2

2          2

2          3

 

Below is what I want:

Id         reg       reg_seq

1          1          1

1          1          1

1          3          2

1          3          2

1          3          2

1          4          3

2          2          1

2          2          1

2          2          1

2          3          2

 

Any advice?

2 REPLIES 2
novinosrin
Tourmaline | Level 20

data want;

set have;

by id reg;

if first.id then reg_seq=1;

else if first.reg the reg_seq+1;

run;

PeterClemmensen
Tourmaline | Level 20
data have;
input Id reg;
datalines;
1 1
1 1
1 3
1 3
1 3
1 4
2 2
2 2
2 2
2 3
;

data want;
   set have;
   by Id reg notsorted;
   if first.Id then reg_seq=1;
   else if first.reg the reg_seq+1;
run;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

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!

Register now

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
  • 2408 views
  • 0 likes
  • 3 in conversation