BookmarkSubscribeRSS Feed
thanikondharish
Fluorite | Level 6

data rawdata ;
input subject date:date9. ;
format date date9. ;
cards;
101 01jan2023
101 01jan2023
101 02jan2023
101 03jan2023
101 03jan2023
101 03jan2023
run;

I have one dataset main group variable is subject and subgroup variable is date . Now how to create group wise sequence number and subgroup wise sequence number in datastep block as well as in proc sql .
Output should be like below:
for example:

subject    date              grp_seq_num         sub_grp_seq_num
101     01jan2023              1                                  1
101     01jan2023               1                                 2
101     02jan2023              1                                   1
101     03jan2023              1                                   1
101     03jan2023               1                                  2
101     03jan2023               1                                   3

102     05jan2023             2                                     1

102     05jan2023             2                                      2

102     06jan2023             2                                      1

2 REPLIES 2
Patrick
Opal | Level 21

"... in datastep block as well as in proc sql"

Why are you asking for explicit and multiple coding approaches? If this is some sort of exercise then I suggest you first put in some own work and then share eventually not yet working code and ask questions based on what you already worked out and what's still missing. 

Kurt_Bremser
Super User

In a data step, it's a breeze. Forget SQL, SQL is not good for sequential operations.

Use a DATA step with BY subject date;

RETAIN both new variables.

At first.subject, increment grp_seq_num.

At first.date, set sub_grp_seq_num to 1, otherwise increment it.

Use Sum Statements to increment.

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