BookmarkSubscribeRSS Feed
sashelp123
Calcite | Level 5

Hello,

 

I am looking for how to find two different variables but only if they exist within the same group. For example I want a list of groups where both A and B exist together. In the example below, that would be Group 1, Group 3, and Group 4. Thank you!

 

GroupCode
1C
2B
3B
4B
5C
3A
1B
4A
5B
1B
8 REPLIES 8
PaigeMiller
Diamond | Level 26

@sashelp123 wrote:

 

I am looking for how to find two different variables but only if they exist within the same group. 


Do you mean you want two observations if two different values of CODE exists in the same group?

 

For example I want a list of groups where both A and B exist together. In the example below, that would be Group 1, Group 3, and Group 4.

 

I don't see how Group 1 meets this criterion.


Show us the desired output

--
Paige Miller
sashelp123
Calcite | Level 5
Hi Paige,

Thanks, you are correct. I mean observations, and my desired output would be Group 3, and Group 4, have both A and B in the same group (not group 1).
PaigeMiller
Diamond | Level 26

So is that the entire question, A and B in the same group?

 

Or is the question really like this:

What groups have both A and B; and what groups have both A and C, and so on?

--
Paige Miller
sashelp123
Calcite | Level 5
Yep that's the entire question, how to find what groups have both A and B?
andreas_lds
Jade | Level 19

Untested:

proc sort data=have out=sorted nodupkey;
   by group code;
run;

data want;
   set sorted;
   by group;
   
   if not first.group and code = 'B' and lag(code) = 'A';
   
   keep group;
run;

EDIT: Simplified the code. 

PaigeMiller
Diamond | Level 26

UNTESTED CODE

 

proc sort data=have;
    by group;
run;
proc transpose data=have out=have_t;
    by group;
    id code;
run;
data want;
    set have_t;
    if whichc('A',of col:)>0 and whichc('B',of col:)>0;
run;

 

If you want tested code, please provide the data as SAS data step code (instructions) and not as screen captures.

--
Paige Miller
Kurt_Bremser
Super User
proc sort data=have;
by group;
run;

data want;
do until (last.group);
  set have;
  by group;
  if code = "A" then _a = 1;
  if code = "B" then _b = 1;
end;
if _a and _b;
keep group;
run;
Ksharp
Super User
data have;
input Group	Code $;
cards;
1	C
2	B
3	B
4	B
5	C
3	A
1	B
4	A
5	B
1	B
;

proc sql;
create table want as
select * from have
 group by group
  having sum(code='A') ne 0 and sum(code='B') ne 0;
quit;

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!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 8 replies
  • 1073 views
  • 4 likes
  • 5 in conversation