3

我有一个包含许多变量的数据集 - 其中许多是字符值。我有以下代码来计算每个变量的缺失值数量:

proc format;
 value $missfmt ' '='Missing' other='Not Missing';
 value  missfmt  . ='Missing' other='Not Missing';
run;


proc freq data=dataname; 
format _CHAR_ $missfmt.; /* apply format for the duration of this PROC */
tables _CHAR_ / missing missprint nocum nopercent;
format _NUMERIC_ missfmt.;
tables _NUMERIC_ / missing missprint nocum nopercent;
run;

但是,这会导致大量输出(如果我打印为 pdf,则为 300 页 pdf),其中 90% 的变量没有缺失值。如何告诉 PROC FREQ 仅显示具有缺失值的表?

4

2 回答 2

4

您可以从 PROC FREQ 中的 NLEVELS 选项中确定哪些变量具有缺失值。所以我的过程是创建一个数据集,其中只保存具有缺失值的变量,然后将它们存储在一个宏变量中,以便只能对它们运行以下 PROC FREQ。这是执行此操作的代码。

/* set up dummy dataset */
data have;
set sashelp.class;
if _n_ in (10,13) then call missing(age,sex);
run;

/* create dataset that holds variables with missing values */
ods select nlevels;
ods output nlevels=miss_vars (where=(nmisslevels>0));
ods noresults;
proc freq data=have nlevels;
run;
ods results;

/* store names in a macro variable */
proc sql noprint;
select tablevar into :missvar separated by ' '
from miss_vars;
quit;

proc format;
 value $missfmt ' '='Missing' other='Not Missing';
 value  missfmt  . ='Missing' other='Not Missing';
run;

proc freq data=have (keep=&missvar.); 
format _CHAR_ $missfmt.; /* apply format for the duration of this PROC */
tables _CHAR_ / missing missprint nocum nopercent;
format _NUMERIC_ missfmt.;
tables _NUMERIC_ / missing missprint nocum nopercent;
run;
于 2013-10-09T09:49:26.950 回答
1

此删除所有空白列:

%macro removeblanks(dataset,output);
/* create dataset that holds variables with missing values */
ods select nlevels;
ods output nlevels=miss_vars (where=(nmisslevels>0 and nnonmisslevels=0));
ods noresults;
proc freq data=&dataset. nlevels;
run;

/* store names in a macro variable */
proc sql noprint;
select tablevar into :missvar separated by ' '
from miss_vars;
quit;

data &output.;
set &dataset.(drop=&missvar.);
run;

%mend removeblanks;`
于 2015-08-04T15:15:24.673 回答