BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
animesh123
Obsidian | Level 7

ID    NAME
1    Animesh

1    Mardi

1    ABC

How to combine this into single variable

Output should be :-  1 Animesh Mardi abc

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
proc sort data=have;
    by ID;
run;

**********************************************************************;
*Use RETAIN and BY group processing to combine the information;
**********************************************************************;
data want_option1;
    set have;
    by ID;
    length combined $100.;
    retain combined;

    if first.ID then
        combined=name;
    else
        combined=catx(', ', combined, name);

    if last.ID then
        output;
run;

Or

 

proc transpose data=have out=wide prefix=name_;
    by ID;
    var name;
run;

data want_option2;
    set wide;
    length combined $100.;
    combined=catx(', ', of name_:);
run;

@animesh123 wrote:

ID    NAME
1    Animesh

1    Mardi

1    ABC

How to combine this into single variable

Output should be :-  1 Animesh Mardi abc


 

View solution in original post

2 REPLIES 2
Reeza
Super User
proc sort data=have;
    by ID;
run;

**********************************************************************;
*Use RETAIN and BY group processing to combine the information;
**********************************************************************;
data want_option1;
    set have;
    by ID;
    length combined $100.;
    retain combined;

    if first.ID then
        combined=name;
    else
        combined=catx(', ', combined, name);

    if last.ID then
        output;
run;

Or

 

proc transpose data=have out=wide prefix=name_;
    by ID;
    var name;
run;

data want_option2;
    set wide;
    length combined $100.;
    combined=catx(', ', of name_:);
run;

@animesh123 wrote:

ID    NAME
1    Animesh

1    Mardi

1    ABC

How to combine this into single variable

Output should be :-  1 Animesh Mardi abc


 

PeterClemmensen
Tourmaline | Level 20

Try this. I added an extra ID to clarify that it handles multiple by groups.

 

data have;
input ID NAME $;
datalines;
1 Animesh 
1 Mardi   
1 ABC     
2 Animesh 
2 Mardi   
2 ABC     
;

data want(drop = name);
   do until (last.ID);
      set have;
      by ID;
      length n $200;
      n = catx(' ', n, name);
   end;
run;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

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!

Register now

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 908 views
  • 1 like
  • 3 in conversation