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

Hi, I am working with some textual data and i want to combine textual rows by ID.

 

Data have:

ID alpha
1 a
1 b
1 c
2 a
2 c
3 a
3 b
4 a

 

Data Want:

1 a,b,c
2 a,c
3 a,b
4 a

Thanks,

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

data have;
input ID	alpha $;
cards;
1	a
1	b
1	c
2	a
2	c
3	a
3	b
4	a
;

data want;
 do until(last.id);
  set have;
  by id;
  length want $100;
  want=catx(',',want,alpha);
 end;
 drop alpha;
run;

View solution in original post

6 REPLIES 6
novinosrin
Tourmaline | Level 20

data have;
input ID	alpha $;
cards;
1	a
1	b
1	c
2	a
2	c
3	a
3	b
4	a
;

data want;
 do until(last.id);
  set have;
  by id;
  length want $100;
  want=catx(',',want,alpha);
 end;
 drop alpha;
run;
rajd1
Quartz | Level 8
Thank you Sir
rajd1
Quartz | Level 8
data have;
input ID	alpha $;
cards;
1	a
1	b
1	c
2	a
2	c
3	a
3	b
4	a
4      a
;

data want;
 do until(last.id);
  set have;
  by id;
  length want $100;
  want=catx(',',want,alpha);
 end;
 drop alpha;
run;

Just a minor addition: What if i had the same alpha for the same id. For example id#4 has a and a twice. I just want it to be displaced as 'a' instead of 'a,a'

novinosrin
Tourmaline | Level 20

Hi @rajd1  Please try the modified and let me know-

 



data have;
input ID	alpha $;
cards;
1	a
1	b
1	c
2	a
2	c
3	a
3	b
4	a
4      a
;

data want;
 do until(last.id);
  set have;
  by id;
  length want $100;
  if ^index(want,strip(alpha)) then want=catx(',',want,alpha);
 end;
 drop alpha;
run;
Sathish_jammy
Lapis Lazuli | Level 10

The following code will help you.

data want(Keep = id newvar);
length newvar $200.;
set have;
by ID;
retain newvar;
if first.ID then newvar=alpha;
else newvar=catx(',',newvar,alpha);
if last.ID;
run;

 

rajd1
Quartz | Level 8
Thanks a lot!

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
  • 6 replies
  • 671 views
  • 3 likes
  • 3 in conversation