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

Starting from a really good post called "concatenate through group by using SAS or PROC SQL, urgent help needed" where there are some great solutions. But I wanted to add in 1 more complication.

Link: https://communities.sas.com/t5/SAS-Procedures/concatenate-through-group-by-using-SAS-or-PROC-SQL-urg...

 

In the original post, the aim is to create a variable which concatenates the groups which a customer is in. For example if Customer A was in Groups 1, 2 and 3 then the variable would read 1/2/3 however it assumed only 1 row per customer per group. What I need is for a customer which has multiple rows in multiple groups with a variable that only reads which groups it is in an not double concatenating any of the groups.

Using table below...

 

Customer

Group

1

A

1

B

2

A

2

B

2

B

2

C

3

A

3

A

 

As such, in the example about the additional column for Customer 1 would read A/B, Customer 2 A/B/C and Customer 3 just A.The previous post with the link above, which might be a great starting point, returns A/B/B/C for customer 2. A great solution and really quite close to what I am looking for, but some of my customer have a lot of rows in each group and looks a little silly.

 

thanks for the help in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

You could create an intermediate table or better a view which contains the unique customer-group pairs and then continue from there.

 

For example:

data have;
input Customer Group $;
cards;
1 A
1 B
2 A
2 B
2 B
2 C
3 A
3 A
;

proc sql;
create view cust_grp as
select distinct customer, group
from have;
quit;

data want;
length grps $30;
do until(last.customer);
  set cust_grp;
  by customer;
  grps=catx('/',grps, group);
end;
drop group;
run;

Or would you like to distinguish between grps='A/B' and grps='B/A' ?

View solution in original post

3 REPLIES 3
kiranv_
Rhodochrosite | Level 12

if you remove duplicates using proc sort and using the solution given in the link should solve the problem

FreelanceReinh
Jade | Level 19

You could create an intermediate table or better a view which contains the unique customer-group pairs and then continue from there.

 

For example:

data have;
input Customer Group $;
cards;
1 A
1 B
2 A
2 B
2 B
2 C
3 A
3 A
;

proc sql;
create view cust_grp as
select distinct customer, group
from have;
quit;

data want;
length grps $30;
do until(last.customer);
  set cust_grp;
  by customer;
  grps=catx('/',grps, group);
end;
drop group;
run;

Or would you like to distinguish between grps='A/B' and grps='B/A' ?

CharlesFowler72
Obsidian | Level 7

No, that has worked a charm. Perfect! Thank you.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 3 replies
  • 3475 views
  • 0 likes
  • 3 in conversation