Proc REPORT将组值(行标题)移到接近总计的位置

时间:2018-10-05 19:25:22

标签: sas proc-report

我有一些结构如下的数据。我需要创建一个包含小计的表,一个总列为TypeA + TypeB的表头以及一个将这些列作为表标题的标题。此外,理想的是在列标题中显示不同的名称,而不是数据集中的变量名称。 我拼凑了一些初步代码,以获取小计和总计,但没有其余部分。

enter image description here

data tabletest;
    informat referral_total $50. referral_source $20.;
    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;

这是所需的输出:

enter image description here

1 个答案:

答案 0 :(得分:1)

DEFINE语句具有选项NOPRINT,该选项导致不显示该列,但是,该列的变量仍然可用(以从左到右的方式)供计算块使用

通过堆积在column语句中,可以自定义列标题和跨度。在非组列的计算块中,Proc REPORT数据向量仅允许访问明细行或合计行的聚合值,因此您需要指定。

此示例代码显示了如何隐藏_total列,以及如何用隐藏的_total值“注入”子和报告总计行中的_source单元格。必须将_source变量加长以适应_total变量中的较长值。

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

  length referral_total $50 referral_source $60;

  informat referral_total $50. referral_source $20.;
  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;

enter image description here