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

Esteemed Advisers

 

I need some help with selectively deleting observations by groups.  Below are exemplar “have”and “want” datasets.  The goal is to retain only one Member that contains “A” in both the Alpha and Beta Groups (which one is retained is irrelevant as they are redundant).  

 

Thanks for taking a look and any advice you have to offer,.

Gene

 

data have;
input Group $ Member $;
Alpha A1
Alpha A2
Alpha B1
Alpha B2
Alpha A3
Alpha C1
Alpha C2
Beta A1
Beta A2
Beta D1
Beta D2
Beta E1
Beta E2
Beta A3
;

data want;
input Group $ Member $;
Alpha A1
Alpha B1
Alpha B2
Alpha C1
Alpha C2
Beta A1
Beta D1
Beta D2
Beta E1
Beta E2
;
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Assuming the data is sorted by GROUP you just to count the number of names MEMBER starts with A per group.

data want;
  set have;
  by group ;
  if first.group then n_a=0;
  if member=:'A' then do;
    n_a+1;
    if n_a>1 then delete;
  end;
run;

View solution in original post

4 REPLIES 4
Ksharp
Super User
data have;
input Group $ Member $;
cards;
Alpha A1
Alpha A2
Alpha B1
Alpha B2
Alpha A3
Alpha C1
Alpha C2
Beta A1
Beta A2
Beta D1
Beta D2
Beta E1
Beta E2
Beta A3
;
proc sql;
create table temp as
select distinct member from have group by member having count(distinct group)>1;

create table want as
select * from have where member in (select member from temp(obs=1))
union
select * from have where member not in (select member from temp);
quit;
Patrick
Opal | Level 21

Here another option. Not as smart as what @Ksharp proposes but should perform better (less sorting and lookup) in case you've got a lot of data and performance is relevant to you.

data have;
  input Group $ Member $;
cards;
Alpha A1
Alpha A2
Alpha B1
Alpha B2
Alpha A3
Alpha C1
Alpha C2
Beta A1
Beta A2
Beta D1
Beta D2
Beta E1
Beta E2
Beta A3
;

proc sort 
  data=have(keep=group member where=(group in ('Alpha','Beta') and member =:'A')) 
  out=inter 
  presorted;
  by group member;
run;

%let memA_sel=; data _null_; merge inter(in=ina where=(group='Alpha')) inter(in=inb where=(group='Beta' )) ; by member; if ina and inb then do; call symputx('memA_sel',member); stop; end; run; data want; set have; if group in ('Alpha','Beta') and member=:'A' and member ne "&memA_sel" then delete; run; proc print data=want; run;

 If you can be sure that your data have is already sorted by group and member (or by member as first variable) then use below code version.

%let memA_sel=;
data _null_;
  merge 
    have(in=ina where=(group='Alpha' and member =:'A'))
    have(in=inb where=(group='Beta'  and member =:'A'))
    ;
  by member;
  if ina and inb then
    do;
      call symputx('memA_sel',member);
      stop;
    end;
run;

data want;
  set have;
  if group in ('Alpha','Beta') and member=:'A' and member ne "&memA_sel" then delete;
run;

 

Tom
Super User Tom
Super User

Assuming the data is sorted by GROUP you just to count the number of names MEMBER starts with A per group.

data want;
  set have;
  by group ;
  if first.group then n_a=0;
  if member=:'A' then do;
    n_a+1;
    if n_a>1 then delete;
  end;
run;
genemroz
Quartz | Level 8

Thanks to all who offered solutions.  All were valid and I chose to accept Tom's only because of its parsimonious use of code.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 4 replies
  • 370 views
  • 1 like
  • 4 in conversation