gpt4 book ai didi

sas - 我如何告诉 PROC FREQ 只显示有缺失值的表?

转载 作者:行者123 更新时间:2023-12-01 11:42:04 26 4
gpt4 key购买 nike

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

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 只显示有缺失值的表?

最佳答案

您可以通过 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;

关于sas - 我如何告诉 PROC FREQ 只显示有缺失值的表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19263384/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com