首页 > 解决方案 > 如何在 SAS 中按类型将观察结果分组在一起

问题描述

我有一个看起来像这样的 SAS 数据集

data test;
    input id type $ quantity cost;
    datalines;
    1 one 2 3
    2 one 3 4.5
    3 two 1 5
    4 three 3 12
    5 two 4 20
    6 three 7 28
    7 one 4 6
    8 two 3 15
    ;
run;

我的目标是根据类型汇总所有观察的数量和成本。最终结果应如下所示。

data test2;
    input type $ combined_quantity combined_cost;
    datalines;
    one 9 13.5
    two 8 40
    three 10 40
    ;
run;

本质上,如果行如

id type quantity cost

4 three 3 12

6 three 7 28

存在,结果应该是

type combined_quantity combined_cost

three 10 40

作为基于相同类型的 2 行的总和

我该如何使用 SAS?任何帮助将不胜感激!

标签: sas

解决方案


proc means data=test nway;
  class type;
  var quantity cost;
output out=test2 (drop=_:) sum=combined_quantity combined_cost;
run;

推荐阅读