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 is hosting free webinars!
Next up: Troy Martin Hughes presents Calling Open-Source Python Functions within SAS PROC FCMP: A Google Maps API Geocoding Adventure on Wednesday April 23.
Register now 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 is hosting free webinars!
Next up: Troy Martin Hughes presents Calling Open-Source Python Functions within SAS PROC FCMP: A Google Maps API Geocoding Adventure on Wednesday April 23.
Register now 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 is hosting free webinars!
Next up: Troy Martin Hughes presents Calling Open-Source Python Functions within SAS PROC FCMP: A Google Maps API Geocoding Adventure on Wednesday April 23.
Register now 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-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

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
  • 1464 views
  • 0 likes
  • 5 in conversation