proc制表缺失值SAS

时间:2016-02-16 06:12:04

标签: sas proc

我有以下代码:

ods tagsets.excelxp file = 'G:\CPS\myworkwithoutmissing.xml'
style = printer;
proc tabulate data = final;
Class Year Self_Emp_Inc Self_Emp_Uninc Self_Emp Multi_Job P_Occupation Full_Part_Time_Status;
table Year, P_Occupation*n;
table Year, (P_Occupation*Self_Emp_Inc)*n;
table Year, (Self_Emp_Inc*P_Occupation)*n;
run;
ods tagsets.excelxp close;

当我运行此代码时,收到以下错误消息:

WARNING: A class, frequency, or weight variable is missing on every observation.
WARNING: A class, frequency, or weight variable is missing on every observation.
WARNING: A class, frequency, or weight variable is missing on every observation.

现在为了避免这个问题,我在类语句的末尾添加了“missing”选项,以便:

class year self_emp_inc ....... Full_Part_Time_Status/ missing;

这解决了问题,因为它没有给我错误消息并创建表。但是,我的图表现在还计算了缺失值的数量,这是我不想要的。例如,我的变量self_emp_inc的值为1和。(缺失)。现在,当我使用缺少的选项运行代码时,我也得到了所有缺失值的P_Occupation计数,但我只想知道self_emp_Inc的值为1时的计数。如何完成该任务?

1 个答案:

答案 0 :(得分:1)

这是SAS中令人沮丧的事情之一,由于某些原因,SAS没有给我们一个“好”选择来解决这个问题。根据您的工作情况,有一些解决方案。

这里真正的问题不是你有缺失 - 在1x1表中(1 var乘以1 var),不包括缺失是你想要的。这是因为您正在调用多个表,并且每个表都会受到另一个表中类变量中的缺失的影响。

因此,最简单的答案通常只是将表拆分为多个proc tabulate语句。在运行时方面,这可能偶尔会太复杂或过于繁重,但我怀疑大多数时候这是最好的解决方案 - 无论如何,它通常都适合我。

由于您只使用n,因此您可以使用缺失构建表格,输出到数据集,然后将其过滤掉并重新打印或导出该数据集。这通常是最简单的解决方案。

你究竟想要做到这一点当然取决于你想要什么。例如:

data test_cars;
  set sashelp.cars;
  if _n_=5 then call missing(make);
  if _n_=7 then call missing(model);
  if _n_=10 then call missing(type);
  if _n_=13 then call missing(origin);
run;

proc tabulate data=test_cars out=test_tabulate(rename=n=count);
  class make model type origin/missing;
  tables (make model type),origin*n;
run;

data test_tabulate_want;
  set test_tabulate;
  if cmiss(of make model type origin)>2 then delete;
  length colvar $200;
  colvar = coalescec(of make model type);
run;

proc tabulate data=test_tabulate_want missing;
  class colvar origin/order=data;
  var count;
  tables colvar,origin*count*sum;
run;

这并不完美,虽然在格式化方面可以做得更好 - 这只是一个简单的例子。

如果您正在使用百分比,当然,这并不完全有效。您需要重构该数据步骤中的百分比 - 这有点工作,但可行 - 或者您需要为每个类变量单独制表。