实现了对sas数据集中全字段的缺失值统计
输出数据集中包含了字段名 缺失值数量 缺失值比率
数据集中如果仅含有字符型 or 数值型字段,则需要对代码进行删减
%let data= 自己的数据集;
%put &data.;
options symbolgen;
data _null_;
if 0 then
set &data. nobs=obs;
array num_vars[*] _numeric_;
array char_vars[*] _character_;
call symputx('num_qty',dim(num_vars));
call symputx('char_qty',dim(char_vars));
call symputx('m_obs',obs);
stop;
run;
%put &num_qty. &char_qty. &m_obs.;
data &data._missing;
set &data. end=finished;
array num_vars[*] _numeric_;
array char_vars[*] _character_;
array num_miss[&num_qty.](&num_qty*0);
array char_miss[&char_qty.](&char_qty*0);
do i=1 to dim(num_vars);
if num_vars(i) eq "" then num_miss(i)+1;
end;
do i=1 to dim(char_vars);
if char_vars(i) eq "" then char_miss(i)+1;
end;
if finished then do;
do i=1 to dim(num_vars);
vname=vname(num_vars(i));
Nmissing=num_miss(i);
rio=num_miss(i)/&m_obs;
output;
end;
do i=1 to dim(char_vars);
vname=vname(char_vars(i));
Nmissing=char_miss(i);
rio=char_miss(i)/&m_obs;
output;
end;
end;
keep vname Nmissing rio;
run;
如果后续需要对缺失值比率超过一定比率的进行删除
proc sql;
select vname into:vname_del separated by ' '
from &data._missing
where rio>=0.7;
quit;
%put &vname_del.;
data &data._2;
set &data.;
drop &vname_del.;
run;
/ref
我用#CSDN#这个app发现了有技术含量的博客,小伙伴们求同去《SAS缺失值筛选,删除比例超过一定阈值的变量/观测》, 一起来围观吧 https://blog.csdn.net/qq_43211866/article/details/93494359