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
"... 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.
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.
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.