sas中的垂直列求和

Vertical column summation in sas

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

数据有:

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

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

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

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

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 来计算总计并使用 much 将结果总计加回去更短的数据步骤等

proc means noprint
    data = have;
    output out= want
    class measure;
    var Jan_total   Feb_total;
run;

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

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

example from here.

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;

结果: