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.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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