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.
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;
Thank you Rezza. Great solutions. Both would work like a charm. Much appreciate your help.
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;
Thanks Richard. Worked like a charm.
@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;
Extraneous SET was an artifact for earlier typing... edited out.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.