BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
hhchenfx
Rhodochrosite | Level 12

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;
1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18
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;

View solution in original post

2 REPLIES 2
Shmuel
Garnet | Level 18
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;
hhchenfx
Rhodochrosite | Level 12

Thank you, Shmuel!

HHCFX

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

Register now

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
  • 784 views
  • 0 likes
  • 2 in conversation