1

我有以下结果,我需要添加。似乎是一个简单的请求,但我已经花了几天时间试图找到解决这个问题的方法。

数据有:

Measure   Jan_total   Feb_total
Startup      100         200
Switcher     300         500

数据要:

Measure   Jan_total   Feb_total
Startup      100         200
Switcher     300         500
Total        400         700

我希望将每列的垂直总和结果单独放置在相应的列下。

有人可以帮我找到这个请求的解决方案吗?

4

3 回答 3

3

要在数据步骤代码中执行此操作,您可以执行以下操作:

data want;
  set have end=end;       * Var 'end' will be true when we get to the end of 'have'.;
  jan_sum + jan_total;    * These 'sum statements' accumulate the totals from each observation.;
  feb_sum + feb_total;
  output;                 * Output each of the original obbservations.;
  if end then do;         * When we reach the end of the input...;
    measure = 'Total';    * ...update the value in Measure...;
    jan_total = jan_sum;  * ...move the accumulated totals to the original vars...;
    feb_total = feb_sum;
    output;               * ...and output them in an additional observation.
  end;
  drop jan_sum feb_sum;   * Get rid of the accumulator variables (this statement can go anywhere in the step).;
run;

你可以通过许多其他方式做到这一点。假设您实际上有所有月份的列,您可能会重写数据步骤代码以使用数组,或者您可能使用 PROC Summary 或 PROC SQL 来计算总计并使用更短的数据步骤将结果总计加回,等等

于 2020-03-02T09:52:15.747 回答
2
proc means noprint
    data = have;
    output out= want
    class measure;
    var Jan_total   Feb_total;
run;
于 2020-03-02T10:28:22.450 回答
0

这取决于这是用于显示还是用于数据集。在数据集中有一个总数通常是没有意义的,它只是用于报告。

PROC PRINT 有一个 SUM 语句,它将总计添加到报表的末尾。PROC TABULATE 还提供了另一种这样的报告机制。

示例从这里。

options obs=10 nobyline;
proc sort data=exprev;
   by sale_type;
run;
proc print data=exprev noobs label sumlabel
           n='Number of observations for the order type: '
           'Number of observations for the data set: ';
   var country order_date quantity price;
   label  sale_type='Sale Type'
          price='Total Retail Price* in USD'
          country='Country' order_date='Date' quantity='Quantity';
   sum price quantity;
   by sale_type;
   format price dollar7.2;
   title 'Retail and Quantity Totals for #byval(sale_type) Sales';
run;
options byline;

结果:

在此处输入图像描述

于 2020-03-02T17:32:38.623 回答