Hi Everyone,
My data is as below, each group_id has multiple row.
I want to join the text within each group_id.
For group_id=1, it has 3 rows with var =x, yy, zzz.
The output should have 2 columns
group_id | join_text
1 | x, yy, zzz
2 | aaa, b
3 | c
Can you please help me to get it?
Thank you so much.
HHCFX
data have;
input group_id var1 $;
datalines;
1 x
1 yy
1 zzz
2 aaa
2 b
3 c
;run;
data want;
set have;
by group_id; /* assuming dataset is sorted by group_ID */
length join_text $100; /* adapt length to max expected */
retain join_text;
if first.group_ID then join_text = var1;
else join_text = catx(',',join_text,var1);
if last.group_id then output;
drop var1;
run;
data want;
set have;
by group_id; /* assuming dataset is sorted by group_ID */
length join_text $100; /* adapt length to max expected */
retain join_text;
if first.group_ID then join_text = var1;
else join_text = catx(',',join_text,var1);
if last.group_id then output;
drop var1;
run;
Thank you, Shmuel!
HHCFX
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.