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.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.