Please post your example data as a data step, e.g. like this:
data have;
length Patient_id $1;
informat Treatment_date mmddyy10.;
format Treatment_date mmddyy10.;
input Patient_id Treatment_date;
cards;
A 1/1/2020
A 1/1/2020
A 5/1/2022
B 1/12/2021
C 1/25/2020
C 6/1t/2021
D 12/31/2021
D 12/31/2021
D 5/25/2022
D 7/10/2022
D 7/10/2022
D 10/4/2022
;run;
I guess (you did not describe the result you wanted, just showed the example) the calculation you want can be done like this (data must be sorted, as is your example data):
data want;
set have;
by Patient_id Treatment_date;
if first.Patient_id then
Treatment_no=1;
else if first.Treatment_date then
Treatment_no+1;
run;
All pretty simple. The statement "Treatment_no+1" is a "sum" statement, and is equivalent to
Treatment_no=sum(Treatment_no,1);
retain Treatment_no;
Please post your example data as a data step, e.g. like this:
data have;
length Patient_id $1;
informat Treatment_date mmddyy10.;
format Treatment_date mmddyy10.;
input Patient_id Treatment_date;
cards;
A 1/1/2020
A 1/1/2020
A 5/1/2022
B 1/12/2021
C 1/25/2020
C 6/1t/2021
D 12/31/2021
D 12/31/2021
D 5/25/2022
D 7/10/2022
D 7/10/2022
D 10/4/2022
;run;
I guess (you did not describe the result you wanted, just showed the example) the calculation you want can be done like this (data must be sorted, as is your example data):
data want;
set have;
by Patient_id Treatment_date;
if first.Patient_id then
Treatment_no=1;
else if first.Treatment_date then
Treatment_no+1;
run;
All pretty simple. The statement "Treatment_no+1" is a "sum" statement, and is equivalent to
Treatment_no=sum(Treatment_no,1);
retain Treatment_no;
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.