0

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

在此处输入图像描述

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;

这是所需的输出:

在此处输入图像描述

4

1 回答 1

1

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

语句中的堆叠column允许您自定义列标题和跨度。在非组列的计算块中,Proc REPORT 数据向量仅允许访问详细信息或总计行的聚合值,因此您需要指定 .

此示例代码显示了 _total 列是如何隐藏的,以及 sub- 和 report-total 行中的 _source 单元格是如何使用隐藏的 _total 值“注入”的。_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;

在此处输入图像描述

于 2018-10-05T20:37:36.697 回答