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

Hello!

 

I have a question about how to identify individuals that are missing a certain value.

 

Below is my dataset:

 

id       class

1        A

1        B

1        C

2        A

2        B

3        B

3        C

 

In this dataset, id 3 is the only one that does not have any class value of A. I would like to identify all individuals that do not have a class value of A. Can anyone guide me how to do this?

 

Thanks!

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

More cleaner

 

data want;
set have;
by id;
retain _class 'A' found;
if first.id then found=.;
if class=_class then found=1;
if last.id and not found;
drop _class found;
run;

View solution in original post

6 REPLIES 6
novinosrin
Tourmaline | Level 20
data have;
input ID    class $;
cards;
1        A
1        B
1        C
2        A
2        B
3        B
3        C
;
run;

data want;
set have;
by id;
retain _class found;
if first.id then found=.;
_class='A';
if class=_class then found=1;
if last.id and not found;
run;
novinosrin
Tourmaline | Level 20

More cleaner

 

data want;
set have;
by id;
retain _class 'A' found;
if first.id then found=.;
if class=_class then found=1;
if last.id and not found;
drop _class found;
run;
SarahW13
Obsidian | Level 7

Thank you!

mkeintz
PROC Star

If each id has exactly zero or one instance of class='A' then a compact way to do this is:

 

data want;
  merge have (where=(class='A') in=afound)  have;
  by id;
  flag=afound;
run;

 

Edited additional note.   This program  will also works for ID's with multiple class A records.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
SarahW13
Obsidian | Level 7

Thank you, mkeintz! In my dataset, there are many ids with multiple instances of class='A' but that's helpful code to know for the future. Thanks!

Ksharp
Super User
data have;
input id       class $;
cards;
1        A
1        B
1        C
2        A
2        B
3        B
3        C
;
run;
proc sql;
select *
 from have
  group by id
   having sum(class='A')=0;
quit;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 2706 views
  • 4 likes
  • 4 in conversation