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.
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.