BookmarkSubscribeRSS Feed
alicezwang96
Fluorite | Level 6

I have a table that looks like this:

ID    Pet
01    Dog
01    Cat
01    Parrot
01    Guinea Pig
02    Dog
02    Guinea Pig
03    Parrot

 

If an ID has a dog and a cat, then I want to exclude ALL records for that ID, regardless of what other animals they have. So my output table would look like:

ID    Pet
02    Dog
02    Guinea Pig
03    Parrot

This is the query that I've tried:

PROC SQL;
CREAT TABLE new_table AS
SELECT * from Pets a
WHERE NOT EXISTS (SELECT *
                  FROM Pets b
                  WHERE b.ID = a.ID
                  AND b.ID = "Dog"
                  AND b.ID = "Guinea Cat"));
RUN;

 

This doesn't seem to be working and isn't actually filtering anything out.

2 REPLIES 2
PaigeMiller
Diamond | Level 26
b.ID = "Dog" AND b.ID = "Guinea Cat"

is always false, the value of ID can't be both "Dog" and "Guinea Cat". Furthermore, variable ID contains numbers, while variable PET contains the types of animals, but even if you replaced ID with PET in the above command, it still won't work. So that's two reasons why this won't work.

 

I would do the following (UNTESTED CODE)

 

proc transpose data=have out=have_t prefix=pet;
    by id;
    var pet;
run;
data want;
    set have_t;
    if whichc('Dog',of pet:)>0 and whichc('Cat',of pet:)>0 then delete;
run;

This doesn't quite produce the exact output you want, it produces a transposed output. You could always transpose WANT to get the exact layout you want.

 

 

--
Paige Miller
novinosrin
Tourmaline | Level 20

Hi @alicezwang96  -

 


data have;
input ID $    Pet & $15.;
cards;
01    Dog
01    Cat
01    Parrot
01    Guinea Pig
02    Dog
02    Guinea Pig
03    Parrot
;


proc sql;
 create table want as
 select *
 from have
 group by id
 having count(distinct case when pet in ('Dog','Cat') then pet else ' ' end)<2;
quit;

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!

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
  • 2 replies
  • 839 views
  • 1 like
  • 3 in conversation