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

I am trying to keep records with results <=50. If one the ID has results >50 I want to remove  the subject from further analysis. How can I do that?

ID

Result

1

50

1

23

2

16

2

7

2

26

3

55

3

30

3

43

3

5

4

50

4

23

 

I want an output like this

ID

Result

1

50

1

23

2

16

2

7

2

26

4

50

4

23

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

FWIW, this appears to be closer to what you are looking for.  However, it can give you a note in the log about more than one data set have repeats of the BY variable:

 

data want;
   merge have have (where=(result > 50) in=delete_me);
   by id;
   if delete_me then delete;
run; 

View solution in original post

4 REPLIES 4
Reeza
Super User
proc sql;
create table want as
select t1.* from have t1
where ID not In (select distinct t2.ID from have t2 where t2.results > 50);
quit;

@hjjijkkl wrote:

I am trying to keep records with results <=50. If one the ID has results >50 I want to remove  the subject from further analysis. How can I do that?

ID

Result

1

50

1

23

2

16

2

7

2

26

3

55

3

30

3

43

3

5

4

50

4

23

 

I want an output like this

ID

Result

1

50

1

23

2

16

2

7

2

26

4

50

4

23

 




hjjijkkl
Pyrite | Level 9
is there another way to do it besides proc sql?
Reeza
Super User

Some other methodologies include:

  • Sort by ID and descending result, so that the first record per ID s the largest. If that's greater than 50 then you need to drop that record otherwise you can keep it.
  • Create a list of all IDs with a result > 50. Then merge the two tables together, using the data set IN option to select only records that are not in the merged table. 
  • DoW loops - fairly complex but allow you to summarize data ahead of time and then merge the results in. Use similar logic as the first approach

All of these essentially have two passes of the data in one form or another but SQL and the DoW loop let it "appear" to be one step.

 


@hjjijkkl wrote:
is there another way to do it besides proc sql?

 

Astounding
PROC Star

FWIW, this appears to be closer to what you are looking for.  However, it can give you a note in the log about more than one data set have repeats of the BY variable:

 

data want;
   merge have have (where=(result > 50) in=delete_me);
   by id;
   if delete_me then delete;
run; 

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!

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
  • 4 replies
  • 612 views
  • 3 likes
  • 3 in conversation