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

I have a Table with variable A and B with 5 obs. I am trying to create a string with values concatenated from the variable B into one string grouped by variable A

 

Example:

A1 = 123; B1 = "XXX";

A2 = 123; B2 = 'YYY';

A3 = 456; B3 = 'ZZZ';

A4 = 456; B4 = 'PPP';

A5 = 456; B5 = 'OOO'

 

Driven by 2 distinct values stored in variable A;  two observations of variable Joined_Variable should be created from variable B.

Joined_Variable = 'XXX, YYY'

Joined_Variable  = 'ZZZ, PPP, OOO'

 

Thank you for your kind help.

1 ACCEPTED SOLUTION

Accepted Solutions
RichardDeVen
Barite | Level 11

Accumulate a concatenation over the group and implicit output of one row per group

data have;
input A B $; datalines;
123 XXX
123 YYY
456 ZZZ
456 PPP
456 OOO
run;

data want(keep=A list);
  do until (last.A);
    set have;
    by A;
    length list $30;     * declarative statement (in PDV column will be after(right of) columns of data set), value is implicitly reset at top of step;
list = catx(', ',list,B); end; run;

View solution in original post

7 REPLIES 7
Apprentice
Calcite | Level 5

Thank you Rezza. Great solutions. Both would work like a charm. Much appreciate your help.

RichardDeVen
Barite | Level 11

Accumulate a concatenation over the group and implicit output of one row per group

data have;
input A B $; datalines;
123 XXX
123 YYY
456 ZZZ
456 PPP
456 OOO
run;

data want(keep=A list);
  do until (last.A);
    set have;
    by A;
    length list $30;     * declarative statement (in PDV column will be after(right of) columns of data set), value is implicitly reset at top of step;
list = catx(', ',list,B); end; run;
Apprentice
Calcite | Level 5

Thanks Richard. Worked like a charm.

ChrisNZ
Tourmaline | Level 20

@RichardDeVen Why not simply

data want(keep=A list);
  do until (last.A);
    set have;
    by A;
    length list $30;    
    list = catx(', ',list,B);
  end;
run;

 

RichardDeVen
Barite | Level 11

Extraneous SET was an artifact for earlier typing... edited out.

ChrisNZ
Tourmaline | Level 20

Another example of the gaping need for a few functions as requested here and here.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 7 replies
  • 6640 views
  • 2 likes
  • 4 in conversation