是否可以使用 SAS 中每个单元格中的值来处理 ods excel?

问题描述

我正在尝试将 SAS 报告保存到 xlsx 文件中。 为此,我使用了“ods excel”功能。 我希望的结果是由具有每一行和列百分比的“proc freq”程序生成的。 这时,问题就出来了。 在结果 xlsx 文件中,计数值和每行和每列百分比值都在相同的单元格中。 这给遵循数据处理步骤带来了困难。 有什么办法可以解决这个问题吗??

*现在

var1 var2
2010 3
3.0
15.0
97
97.0
85.0

想要

var1 var2
2010 3 15
3.0 97.0
15.0 85.0

解决方法

尝试创建一个输出表并使用 proc export 将其导出。例如:

proc freq data=sashelp.cars noprint;
    tables make / out=freq_table;
run;

proc export 
    data = freq_table
    file = "/folder/myfile.xlsx"
    dbms = xlsx
    replace
    ;
run;

如果您想要自定义外观的报告,则需要在输出表中使用 proc report 并使用 ods excel 代替

,

您似乎在 Proc FREQ 中生成交叉表。

您可以捕获用于创建您不喜欢的输出的 ODS 数据,并对其进行操作以创建您喜欢的输出。

ODS OUTPUT 在转置后会创建一个表格,该表格可以简单地进行 Proc PRINTED 并包含您想要的重复。

示例:

ods listing;
ods select none;
ods output CrossTabFreqs=ctf;
proc freq data=sashelp.baseball;
  table position * division;
  where position like '%B';
  title "FREQ";
run;
ods select all;

data have;
  set ctf;
  if substr(_type_,1,1) = '0' then position = 'Total';
  if substr(_type_,2,1) = '0' then division = 'Total';
run;

proc transpose data=have out=want;
  by position notsorted;
  var frequency percent rowpercent colpercent;
  id division;
run; 

ods excel file='ct.xlsx' style=plateau;

proc print data=want(where=(N(east,west,total)));
run;

ods _all_ close;

enter image description here

注意:

如果您捕获交叉表输出(每个 Stu),您可能必须在 Excel 中创建一个数据透视表,即使这样也会创建您不喜欢的合并单元格。 Excel 中的数据透视表确实具有内置的过滤 UI。