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

So i have a data set that has multiple observations for some users, but I only want 1 observation for each unique user ID depending on the variable 'consent'.  If a member has multiple Yes (Y) for consent, I want to keep one Y observation, if they have multiple No (N) for consent, I want to keep one N observation, and if they have some Y and some N, I only want to keep one Y observation.  Example:

 

HAVE:

User ID    Consent

1                 Y

2                 Y

2                N

3                N

3                N

4                Y

4                Y

4                N

 

WANT

User ID  Consent

1              Y

2             Y

3             N

4             Y

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20
data have;
input UserID Consent $;
datalines;
1 Y
2 Y
2 N
3 N
3 N
4 Y
4 Y
4 N
;

proc sort data=have;
    by UserID Consent;
run;

data want;
    set have;
    by UserID;
    if last.UserID;
run;

View solution in original post

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20
data have;
input UserID Consent $;
datalines;
1 Y
2 Y
2 N
3 N
3 N
4 Y
4 Y
4 N
;

proc sort data=have;
    by UserID Consent;
run;

data want;
    set have;
    by UserID;
    if last.UserID;
run;
novinosrin
Tourmaline | Level 20

data have;
input UserID    Consent $;
cards;
1                 Y
2                 Y
2                N
3                N
3                N
4                Y
4                Y
4                N
;


proc sql;
create table want as
select distinct *
from have
group by userid
having max(consent)=consent;
quit;

novinosrin
Tourmaline | Level 20

data have;
input UserID    Consent $;
cards;
1                 Y
2                 Y
2                N
3                N
3                N
4                Y
4                Y
4                N
;
data want;
do until(last.userid);
set have;
by userid;
if consent='Y' and _f=. then do; output;_f=1;end;
else if _f=. and last.userid then output;
end;
drop _f;
run;

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