【问题标题】:Vertical column summation in sassas中的垂直列求和
【发布时间】:2020-03-02 08:36:12
【问题描述】:

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

数据有:

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

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

有人可以帮我解决这个请求吗?

【问题讨论】:

  • 嗨,Sahil,欢迎来到 StackOverflow。考虑在您的问题中解释一些背景。例如说它上下文(HTML CSS、React、Android 等),以便用户可以为您提供有意义的答案。另外请添加一些您正在进行的工作的代码,以便更好地理解问题。
  • 这能回答你的问题吗? Return dataset of column sums in SAS
  • 如果您得到完美答案,请考虑将其标记为已接受。您可以通过按答案左上角的复选标记来完成。投票也是一种选择。我之所以提到这一点,是因为您是 stackoverflow 的新手,并且由于您在评论中表达了您的赞赏,您可以通过给回答者的声誉评分来给予他的信任。不要为此感到压力,因为您总是可以等待更好的答案,或者它可能对您不够满意。

标签: sas column-sum


【解决方案1】:

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

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

【讨论】:

  • 非常感谢您的解释和详细的代码。它给了我需要的最终输出。欣赏它。
  • 作为问题的作者,您可以选择将答案标记为您选择的答案,从而为该答案的作者再提供 15 点声望。
  • @Chris 你能帮我多忙吗?如果我有相同的数据结构,但我需要计算一系列变量的总和,就像上面的情况一样?在解决方案中,您提到了单个变量名称,例如 jan_total、feb_total 等。我有 14 个月的此类运行数据,需要求和,并且需要在每个类别下方显示总计。有什么想法吗?
【解决方案2】:
proc means noprint
    data = have;
    output out= want
    class measure;
    var Jan_total   Feb_total;
run;

【讨论】:

    【解决方案3】:

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

    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;
    

    结果:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-21
      • 2014-04-15
      相关资源
      最近更新 更多