【问题标题】:how to show the dynamic results with filter in the final report (.xlsx file)如何在最终报告(.xlsx 文件)中使用过滤器显示动态结果
【发布时间】:2020-10-26 18:51:07
【问题描述】:

我正在编写一份报告(.xlsx 文件)。 数据有 3 列用于销售(销售 1、销售 2 和销售 3)。 在最终输出(.xlsx 文件)中,我喜欢显示:

  1. 列之间总计的差异,即列sale3中的-36(=1008-1044)等请参见第一张图片

  2. 我希望看到过滤后的最终报告(过滤器负载='Y')请看第二张图片 - sale3 的总计为 848(sale2 为 860,sale1 为 871),列差异为 -12,列 %change 为 (1.40%),最后一行为 -11 (=848-860) 和-11 (=860-871) 总计差。

     data sales;
     input area load $ prod : $ sale1 sale2 sale3;
      diff=sale3-sale2;
     datalines;
     1 Y p1   109 117 138 
     1 N p1   23  29  20 
     1 Y p2   78  70  68
     1 N p2   63  19  22 
     2 Y p1   49  36  32 
     2 N p1   50  39  44  
     2 Y p3   138 157 158 
     2 N p3   110 126 107 
     3 Y p2   251 267 259  
     3 N p2   182 184 160 
     ;
     run;
    
     ods excel close;
     ods excel file="/C:/data/t1.xlsx"
       options (sheet_name="tab1" frozen_headers='3' frozen_rowheaders='2'       
                embedded_footnotes='yes' autofilter='1-8'); 
     proc report data=sales  nocenter;    
      column area load prod sale1 sale2 sale3 diff change;
      define area -- diff/ display; 
      define sale1-- diff / analysis sum format=comma12. style(column)= 
             [cellwidth=.5in];
      define change / computed format=percent8.2 '% change' style(column)= 
             [cellwidth=.8in];
    
     compute change;
      change = diff.sum/sale2.sum;
       if change >= 0.1 then call define ("change",'STYLE','STYLE=[color=red 
           fontweight=bold]');
       if change <= -0.1 then call define ("change",'STYLE','STYLE=[color=blue 
           fontweight=bold]');
     endcomp;
    
     rbreak after / summarize style=[background=lightblue font_weight=bold];
     run;
     ods excel close;  
    

第一张图片显示报告(未应用过滤器)如下所示:

第二张图片显示(应用了过滤器) - 这是我想在报告中看到的

【问题讨论】:

  • ODS EXCEL 目的地没有用于预选自动过滤器的功能或选项。自动筛选项目选择将涉及 VBA 代码或“入侵”到 .xlsx 文件中
  • 这是否意味着 SAS 无法进行自动过滤?
  • ODS EXCEL 可以输出启用自动过滤的表格数据。您不能指定最初选择的项目(即应用了过滤器)
  • 直接通过 SAS,没有。但是通过 VBS 执行此操作然后从 SAS 或类似的方式调用该脚本是相当简单的。您最近是否也在 community.sas.com 上问过这个问题?有人做过,我不记得官方的答案是什么,但很可能是相似的。
  • 我没有在其他任何地方问过这个问题。顺便说一句,我可以做最后一行(sale2 列中的-9,sale3 列中的-36) - 即总计之间的差异?

标签: sas report


【解决方案1】:

可以通过将REPORT 计算的硬总和替换为通过调用定义样式将tagattr 设置为Excel 公式传递到单元格的公式,从而使报表总行具有过滤器意识。

Excel 函数SUBTOTAL(9,range) 可识别过滤器,代码9 表示计算 SUM。 ExcelINDIRECT 用于计算总行上方的列单元格范围。

例子:

proc report data=sales  nocenter;
  column area load prod sale1 sale2 sale3 diff change;
  define area -- diff/ display;
  define sale1-- diff / analysis sum format=comma12. style(column)= [cellwidth=.5in];
  define change / computed format=percent8.2 '% change' style(column)= [cellwidth=.8in];

  compute change;
    change = diff.sum/sale2.sum;
    if change >=  0.1 then call define ("change",'STYLE','STYLE=[color=red fontweight=bold]');
    if change <= -0.1 then call define ("change",'STYLE','STYLE=[color=blue fontweight=bold]');
  endcomp;

  * inject Excel formula into summary cell;
  compute after;
    call define ('sale1.sum', 'style', "style=[tagattr='formula:=SUBTOTAL(9,INDIRECT(CONCATENATE(ADDRESS(2,COLUMN()),"":"",ADDRESS(ROW()-1,COLUMN()))))']");
    call define ('sale2.sum', 'style', "style=[tagattr='formula:=SUBTOTAL(9,INDIRECT(CONCATENATE(ADDRESS(2,COLUMN()),"":"",ADDRESS(ROW()-1,COLUMN()))))']");
    call define ('sale3.sum', 'style', "style=[tagattr='formula:=SUBTOTAL(9,INDIRECT(CONCATENATE(ADDRESS(2,COLUMN()),"":"",ADDRESS(ROW()-1,COLUMN()))))']");
  endcomp;

  rbreak after / summarize style=[background=lightblue font_weight=bold];

run;

如 cmets 中所述,过滤器选择默认为列中的所有值,您不能指定预选。

不确定如何将文本/公式添加到程序输出之外的特定单元格,但不太容易。

【讨论】:

    猜你喜欢
    • 2021-02-02
    • 1970-01-01
    • 2021-01-13
    • 2020-09-12
    • 1970-01-01
    • 2012-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多