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

Hi

I need help removing observations depending on previous observations.

I have

ID   HA

1     1

1     3

2     3

2     3

2     3

3     2

3     3

3     3

4     1

4     2

4     3

 

I want

ID   HA

1     1

2     3

2     3

2     3

3     2

4     1

 

For each ID I want to keep the observation where HA=1 or HA=2. If one ID has both HA=1 and HA=2, I only want to keep the one where HA=1. If an ID has neither HA=1 nor 2 (ie. only HA=3), then I want to keep all the observations with HA=3.

Any suggestions?

1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
Super User

You could do it by sorting, then using first.id to pick off the first record.  If it's a 1 or 2, you output it.  If it's a 3, that means all records for that ID are 3's, and you want to output them all. 

 

So you could do it like:

data have ;
 input ID HA ;
 cards ;
1     1
1     3
2     3
2     3
2     3
3     2
3     3
3     3
4     1
4     2
4     3
;
run ;

proc sort data=have ;
  by id ha ;
run ;

data want ;
  set have ;
  by id ;    
  retain allthrees ;
  if first.id then do ;
    if HA IN (1,2) then output ;
    allthrees=(HA=3) ;
  end ;
  if allthrees then output ;
run ;
The Boston Area SAS Users Group (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

@RTN wrote:

 

For each ID I want to keep the observation where HA=1 or HA=2. If one ID has both HA=1 and HA=2, I only want to keep the one where HA=1. If an ID has neither HA=1 nor 2 (ie. only HA=3), then I want to keep all the observations with HA=3.

 


What about the third case, where one ID has either HA=1 or HA=2, but not both?

--
Paige Miller
RTN
Calcite | Level 5 RTN
Calcite | Level 5
When one ID has neither HA=1 nor HA=2, then I want to keep all observations.
PaigeMiller
Diamond | Level 26

@RTN wrote:
When one ID has neither HA=1 nor HA=2, then I want to keep all observations.

Actually, that wasn't my question.

--
Paige Miller
novinosrin
Tourmaline | Level 20

Hi @RTN  Perhaps, what genius @mkeintz calls this "Readymeals" solution might just suffice 🙂 . So here's the lazy one-


data have;
input ID   HA;
cards;
1     1

1     3

2     3

2     3

2     3

3     2

3     3

3     3

4     1

4     2

4     3
;
proc sql;
create table want as
select *
from have
group by id
having min(ha)=ha;
quit;

  

Quentin
Super User
Are you ok with sorting the data? Can I assume that the only possible values for HA are 1, 2, 3?
The Boston Area SAS Users Group (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.
RTN
Calcite | Level 5 RTN
Calcite | Level 5
I am ok with sorting the data and the only values for HA are 1,2 or 3.
Quentin
Super User

You could do it by sorting, then using first.id to pick off the first record.  If it's a 1 or 2, you output it.  If it's a 3, that means all records for that ID are 3's, and you want to output them all. 

 

So you could do it like:

data have ;
 input ID HA ;
 cards ;
1     1
1     3
2     3
2     3
2     3
3     2
3     3
3     3
4     1
4     2
4     3
;
run ;

proc sort data=have ;
  by id ha ;
run ;

data want ;
  set have ;
  by id ;    
  retain allthrees ;
  if first.id then do ;
    if HA IN (1,2) then output ;
    allthrees=(HA=3) ;
  end ;
  if allthrees then output ;
run ;
The Boston Area SAS Users Group (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.
Ksharp
Super User

If understood your question correctly .

 

data have ;
 input ID HA ;
 cards ;
1     1
1     3
2     3
2     3
2     3
3     2
3     3
3     3
4     1
4     2
4     3
;
run ;
data want;
 set have;
 by id ha;
 if first.id then n=0;
 n+first.ha;
 if n=1;
 drop n;
run;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 8 replies
  • 1148 views
  • 0 likes
  • 5 in conversation