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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 6 replies
  • 1358 views
  • 4 likes
  • 4 in conversation