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

Data Set

ID      Date            Variable

111    12/7/2021     A

111    12/7/2021     B

111    12/7/2021     C

222    12/6/2021    A

222     12/7/2021   A

 

Expected Result

ID   Date          Variable

111 12/7/2021  A, B, C

222  12/6/2021 A

222  12/7/2021 A

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Typically two ways - one is a data step and combine as you go down the rows and output on the last group record. This utilizes RETAIN and BY group processing. 
The second method is to transpose the data into a wide format using PROC TRANSPOSE and then use CATX() to combine the data.

Both are illustrated below.

 

*create sample data for demonstration;
data have;
    infile cards dlm='09'x;
    input OrgID Product $   States $;
    cards;
1   football    DC
1   football    VA
1   football    MD
2   football    CA
3   football    NV
3   football    CA
;
run;

*Sort - required for both options;
proc sort data=have;
    by orgID;
run;

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

    if first.orgID then
        combined=states;
    else
        combined=catx(', ', combined, states);

    if last.orgID then
        output;
run;

**********************************************************************;
*Transpose it to a wide format and then combine into a single field;
**********************************************************************;
proc transpose data=have out=wide prefix=state_;
    by orgID;
    var states;
run;

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

https://gist.github.com/statgeek/d583cfa992bf56da51d435165b07e96a

 


@annaleticia wrote:

Data Set

ID      Date            Variable

111    12/7/2021     A

111    12/7/2021     B

111    12/7/2021     C

222    12/6/2021    A

222     12/7/2021   A

 

Expected Result

ID   Date          Variable

111 12/7/2021  A, B, C

222  12/6/2021 A

222  12/7/2021 A


 

View solution in original post

3 REPLIES 3
Reeza
Super User

Typically two ways - one is a data step and combine as you go down the rows and output on the last group record. This utilizes RETAIN and BY group processing. 
The second method is to transpose the data into a wide format using PROC TRANSPOSE and then use CATX() to combine the data.

Both are illustrated below.

 

*create sample data for demonstration;
data have;
    infile cards dlm='09'x;
    input OrgID Product $   States $;
    cards;
1   football    DC
1   football    VA
1   football    MD
2   football    CA
3   football    NV
3   football    CA
;
run;

*Sort - required for both options;
proc sort data=have;
    by orgID;
run;

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

    if first.orgID then
        combined=states;
    else
        combined=catx(', ', combined, states);

    if last.orgID then
        output;
run;

**********************************************************************;
*Transpose it to a wide format and then combine into a single field;
**********************************************************************;
proc transpose data=have out=wide prefix=state_;
    by orgID;
    var states;
run;

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

https://gist.github.com/statgeek/d583cfa992bf56da51d435165b07e96a

 


@annaleticia wrote:

Data Set

ID      Date            Variable

111    12/7/2021     A

111    12/7/2021     B

111    12/7/2021     C

222    12/6/2021    A

222     12/7/2021   A

 

Expected Result

ID   Date          Variable

111 12/7/2021  A, B, C

222  12/6/2021 A

222  12/7/2021 A


 

ballardw
Super User

What do you expect to do with that data set that you can't do with the original?

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 3 replies
  • 24437 views
  • 1 like
  • 3 in conversation