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

I'm using SAS Enterprise guide and switching over from Hyperion Interactive Reporting.  I have two data columns that I'm trying to create a summary column of all the unique responses from another column.  In the example below I am trying to create the "Colors_By_Animal" column where all of the unique options of colors per animal are rolled together and separated by commas in a third column.  Duplicate colors per animal are only shown once and blanks are ignored.  Either option of adding the column in with all the data or creating a summary table with just one line per animal would work for me.  Any ideas?  Please and Thank you.

 

Cheesiepoof05_0-1663960410995.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Here are two different methods outlined for solving this type of problem. 

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

 

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;

FYI - if you provide images and people want to help answer your question with code that is tested, they would have to type out your data manually. Instead provide data at minimum as text, preferably as a data step if possible.

 


@Cheesiepoof05 wrote:

I'm using SAS Enterprise guide and switching over from Hyperion Interactive Reporting.  I have two data columns that I'm trying to create a summary column of all the unique responses from another column.  In the example below I am trying to create the "Colors_By_Animal" column where all of the unique options of colors per animal are rolled together and separated by commas in a third column.  Duplicate colors per animal are only shown once and blanks are ignored.  Either option of adding the column in with all the data or creating a summary table with just one line per animal would work for me.  Any ideas?  Please and Thank you.

 

Cheesiepoof05_0-1663960410995.png

 


 

View solution in original post

5 REPLIES 5
Reeza
Super User

Here are two different methods outlined for solving this type of problem. 

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

 

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;

FYI - if you provide images and people want to help answer your question with code that is tested, they would have to type out your data manually. Instead provide data at minimum as text, preferably as a data step if possible.

 


@Cheesiepoof05 wrote:

I'm using SAS Enterprise guide and switching over from Hyperion Interactive Reporting.  I have two data columns that I'm trying to create a summary column of all the unique responses from another column.  In the example below I am trying to create the "Colors_By_Animal" column where all of the unique options of colors per animal are rolled together and separated by commas in a third column.  Duplicate colors per animal are only shown once and blanks are ignored.  Either option of adding the column in with all the data or creating a summary table with just one line per animal would work for me.  Any ideas?  Please and Thank you.

 

Cheesiepoof05_0-1663960410995.png

 


 

Cheesiepoof05
Obsidian | Level 7
Thank you for the quick solution. It's working exactly as I need. Sorry about not including the data properly. This was my first question and I thought it was be cleaner for people to see with the formatting but now I know for the future. Thanks again!
Cheesiepoof05
Obsidian | Level 7
I'm using option 1 and just realized I'm getting duplicates if there was more than one of the same color. IE for bears, I'm getting two instances of black in the list.

Bears Black, Black, Brown, Red
Reeza
Super User

You can add a step before combining to remove duplicates:

 

proc sort data=have out=noduplicates nodupkey;
by animal individual_color;
run;

Or another method:

 

*Sort - required for both options;
proc sort data=have;
    by orgID states;
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 if first.states then 
         combined=catx(', ', combined, states);

    if last.orgID then
        output;
run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 5 replies
  • 406 views
  • 6 likes
  • 2 in conversation