I have a table with three variables: patient, time, drug. Let us say there are two patients. First patient received drug A at time 1 followed by drug B at time 2. Second patient received drug B at time 1 followed by drug A at time 2. I want to accumulate a substring for the group variable, such that patient 1 belongs to group "AB" and patient 2 belong to group "BA". Any suggestions are welcome!
data want;
input patient time drug $ group $;
datalines;
1 1 A AB
1 2 B AB
2 1 B BA
2 2 A BA
;
run;
/*Assuming there are no duplicated obs in your data*/
data have;
input patient time drug $;
datalines;
1 1 A
1 2 B
2 1 B
2 2 A
;
run;
data want;
do until(last.patient);
set have;
by patient;
length group $ 10;
group=cats(group,drug);
end;
do until(last.patient);
set have;
by patient;
output;
end;
run;
/*Assuming there are no duplicated obs in your data*/
data have;
input patient time drug $;
datalines;
1 1 A
1 2 B
2 1 B
2 2 A
;
run;
data want;
do until(last.patient);
set have;
by patient;
length group $ 10;
group=cats(group,drug);
end;
do until(last.patient);
set have;
by patient;
output;
end;
run;
@Ksharp , thank you, this is very helpful! I would like to have "ABB" group for duplicates, could you please advice?:
data want;
input patient time drug $ group $;
datalines;
1 1 A ABB
1 2 B ABB
1 3 B ABB
2 1 B BA
2 2 A BA
;
run;
Pass through each ID twice, once to build GROUP, once to output it:
data have;
input patient time drug $;
datalines;
1 1 A
1 2 B
2 1 B
2 2 A
run;
data want;
set have (in=firstpass)
have (in=secondpass);
by id;
retain group ' ' ; /*As many blanks as needed for group */
if first.id then group=drug;
else if firstpass then group=cats(group,drug);
if secondpass;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.