Proc REPORT 移动组值(行 header)更接近总计

Proc REPORT move group value (row header) closer to totals

我有一些结构如下的数据。我需要创建一个带有小计的 table,一个 TypeA + TypeB 的总计列和一个跨越列的 header 作为 table 标题。此外,最好在列标题中显示不同的名称,而不是数据集中的变量名称。 我拼凑了一些初步代码来获得小计和总计,但没有得到其余部分。

data tabletest;
    informat referral_total . referral_source .;
    infile datalines delimiter='|';
    input referral_total referral_source TypeA TypeB ;
    datalines;
    Long Org Name | SubA | 12 | 5
    Long Org Name | SubB | 14 | 3
    Longer Org Name | SubC | 0 | 1
    Longer Org Name | SubD | 4 | 12
    Very Long Org | SubE | 3 | 11
    Very Long Org | SubF | 9 | 19
    Very Long Org | SubG | 1 | 22
    ;
    run;

我写的代码:

proc report data=tabletest nofs headline headskip;
column referral_total referral_source TypeA TypeB;
define referral_total / group ;
define referral_source / group;
define TypeA / sum ' ';
define TypeB / sum ' ';
break after referral_total /  summarize style={background=lightblue font_weight=bold };
rbreak after /summarize;
compute referral_total;
    if _break_ = 'referral_total' then
    do;
    referral_total = catx(' ', referral_total, 'Total');
    end;
else if _break_ in ('_RBREAK_') then
    do;
    referral_total='Total';
    end;
endcomp;
run;

这是期望的输出:

DEFINE 语句有一个选项 NOPRINT 会导致不呈现该列,但是,它的变量仍然可用(以从左到右的方式)用于计算块。

column 语句中的堆叠允许您自定义列 headers 和跨度。在 non-group 列的计算块中,Proc REPORT 数据向量仅允许访问明细行或总计行的聚合值,因此您需要指定 .

此示例代码显示了 _total 列是如何隐藏的,以及子总计和报告总计行中的 _source 单元格 'injected' 具有隐藏的 _total 值。必须延长 _source 变量以容纳 _total 变量中更长的值。

data tabletest;
  * ensure referral_source big enough to accommodate _total || ' TOTAL';

  length referral_total  referral_source ;

  informat referral_total . referral_source .;
  infile datalines delimiter='|';
  input referral_total referral_source TypeA TypeB ;
datalines;
Long Org Name | SubA | 12 | 5
Long Org Name | SubB | 14 | 3
Longer Org Name | SubC | 0 | 1
Longer Org Name | SubD | 4 | 12
Very Long Org | SubE | 3 | 11
Very Long Org | SubF | 9 | 19
Very Long Org | SubG | 1 | 22
run;

proc report data=tabletest;
  column 
  ( 'Table 1 - Stacking gives you custom headers and hierarchies'
    referral_total 
    referral_source 
    TypeA TypeB
    TypeTotal
  );
  define referral_total / group noprint;                 * hide this column;
  define referral_source / group;
  define TypeA / sum 'Freq(A)';                          * field labels are column headers;
  define TypeB / sum 'Freq(B)';
  define TypeTotal / computed 'Freq(ALL)';               * specify custom computation;
  break after referral_total /  summarize style={background=lightblue font_weight=bold };
  rbreak after /summarize;

  /*
   * no thanks, doing this in the _source compute block instead;
  compute referral_total;
      if _break_ = 'referral_total' then
      do;
      referral_total = catx(' ', referral_total, 'Total');
      end;
  else if _break_ in ('_RBREAK_') then
      do;
      referral_total='Total';
      end;
  endcomp;
  */

  compute referral_source;
    * the referral_total value is available because it is left of me. It just happens to be invisible;
    * at the break lines override the value that appears in the _source cell, effectively 'moving it over';
    select (_break_);
      when ('referral_total') referral_source = catx(' ', referral_total, 'Total');
      when ('_RBREAK_') referral_source = 'Total';
      otherwise;
    end;
  endcomp;

  compute TypeTotal;
    * .sum is needed because the left of me are groups and only aggregate values available here;
    TypeTotal = Sum(TypeA.sum,TypeB.sum); 
  endcomp;
run;