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

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
Barite | Level 11

Thank you, Shmuel!

HHCFX

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 346 views
  • 0 likes
  • 2 in conversation