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?
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;
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;
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;
Thx so much - worked
this works beautifully. would you be able to explain what max (case means pls?
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;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.