Hi folks,
I have a data with missing and I'd like to flag those individuals with missing data across all the rows as shown in the snippet below.
Any suggestions appreciated. Thanks in advance.
DATA HAVE;
INPUT ID DIAGNOSIS FLAG;
CARDS;
1 3 0
1 1 0
1 99 0
1 99 0
1 99 0
1 99 0
1 1 0
1 99 0
1 3 0
1 99 0
2 99 1
2 99 1
2 99 1
2 99 1
2 99 1
;
PROC PRINT; RUN;
DATA HAVE;
INPUT ID DIAGNOSIS;* FLAG;
CARDS;
1 3 0
1 1 0
1 99 0
1 99 0
1 99 0
1 99 0
1 1 0
1 99 0
1 3 0
1 99 0
2 99 1
2 99 1
2 99 1
2 99 1
2 99 1
;
proc sql;
create table want as
select *,sum(DIAGNOSIS=99)=n(id) as flag
from have
group by id;
quit;
So 99 is indicating 'missing' here?
You can do something like this
data have;
input id diagnosis;
cards;
1 3
1 1
1 99
1 99
1 99
1 99
1 1
1 99
1 3
1 99
2 99
2 99
2 99
2 99
2 99
;
data want(drop=c);
c=0;
do _n_ = 1 by 1 until (last.id);
set have;
by id;
if diagnosis=99 then c=c+1;
end;
do until (last.id);
set have;
by id;
flag=ifn(c=_N_, 1, 0);
output;
end;
run;
DATA HAVE;
INPUT ID DIAGNOSIS;* FLAG;
CARDS;
1 3 0
1 1 0
1 99 0
1 99 0
1 99 0
1 99 0
1 1 0
1 99 0
1 3 0
1 99 0
2 99 1
2 99 1
2 99 1
2 99 1
2 99 1
;
proc sql;
create table want as
select *,sum(DIAGNOSIS=99)=n(id) as flag
from have
group by id;
quit;
Thanks both of you. Excellent solutions.
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!
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.
Ready to level-up your skills? Choose your own adventure.