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

Dataset

 

ID   Result

1   pos

1   neg

1   pos

1   neg

2   neg

2   neg

2   neg

 

I want to create a flag for an ID if at least one or more of the Result values for that ID is pos, so I want it to look like

 

ID   Result   Flag

1   pos.        1

1   neg.         1

1   pos.         1

1   neg.         1

2   neg          0

2   neg           0

2   neg           0

 

How do I do this?

 

1 ACCEPTED SOLUTION

Accepted Solutions
kiranv_
Rhodochrosite | Level 12

one more way

proc sql;
create table want as
select *,
max(case when result ='pos' then 1 else 0 end) as flag
from have
group by id;
quit;

View solution in original post

5 REPLIES 5
novinosrin
Tourmaline | Level 20
data have;
input ID   Result $;
cards;
1   pos
1   neg
1   pos
1   neg
2   neg
2   neg
2   neg
;

proc sql;
create table want as
select *,sum(upcase(result)='POS')>0 as flag
from have
group by id;
quit;
kiranv_
Rhodochrosite | Level 12

one more way

proc sql;
create table want as
select *,
max(case when result ='pos' then 1 else 0 end) as flag
from have
group by id;
quit;
KPCklebspn
Obsidian | Level 7

this works beautifully. would you be able to explain what max (case means pls?

novinosrin
Tourmaline | Level 20
data have;
input ID   Result $;
cards;
1   pos
1   neg
1   pos
1   neg
2   neg
2   neg
2   neg
;

data want;
merge have(in=in1) have(where=(upcase(result)='POS') in=in2);
by id;
flag=in1 and in2;
run;

flag=in2; alone will do as a matter of fact

 

data want1;
merge have(in=in1) have(where=(upcase(result)='POS') in=in2);
by id;
flag=in2;
run;

 

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

Register now

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 2277 views
  • 1 like
  • 3 in conversation