SAS中的年度条件总和

时间:2018-10-28 12:45:40

标签: sas

我有一个下表

+------+------+------+------+------+-----+
|  Yr  | col1 | col2 | col3 | col4 | PQR |
+------+------+------+------+------+-----+
| 2012 |    1 |    0 |    1 |    1 |   2 |
| 2012 |    0 |    1 |    0 |    0 |   4 |
| 2013 |    1 |    1 |    1 |    1 |   6 |
| 2014 |    0 |    0 |    0 |    0 |   8 |
| 2012 |    1 |    0 |    1 |    1 |   7 |
| 2013 |    0 |    1 |    0 |    0 |   3 |
| 2014 |    1 |    0 |    1 |    1 |   2 |
| 2012 |    0 |    1 |    0 |    0 |  10 |
| 2014 |    0 |    0 |    1 |    0 |  12 |
| 2014 |    0 |    0 |    0 |    0 |   5 |
+------+------+------+------+------+-----+

我想要的输出如下

+------+-------+------+------+------+
|      | Total | 2012 | 2013 | 2014 |
+------+-------+------+------+------+
| col1 |    17 |    9 |    6 |    2 |
| col2 |    23 |   14 |    9 |    0 |
| col3 |    29 |    9 |    6 |   14 |
| col4 |    17 |    9 |    6 |    2 |
+------+-------+------+------+------+

对于我的输出表中的第col1

The column `Total` is `SUM(PQR)` when `col1` is 1 my input table

The value `17` is `SUM(PQR)` when `col1` is 1 in my input table

The value in  col `2012` is `SUM(PQR)` when `col1` is 1 and `Yr=2012` in my input table

The value `9` is `SUM(PQR)` when `col1` is 1 and `Yr=2012` in my input table

6为1且2013SUM(PQR)时,col1列中的Yr2013

希望了解了解输出表的过程

我想通过SAS实现以上结果。 任何帮助将不胜感激

1 个答案:

答案 0 :(得分:2)

将数据转换为分类形式,并使用PQR作为总和中的权重。 Proc TABULATE非常擅长创建此类列表。

data have;
infile datalines dlm='|'; input
   Yr    col1   col2   col3   col4   PQR ; datalines;
| 2012 |    1 |    0 |    1 |    1 |   2 |
| 2012 |    0 |    1 |    0 |    0 |   4 |
| 2013 |    1 |    1 |    1 |    1 |   6 |
| 2014 |    0 |    0 |    0 |    0 |   8 |
| 2012 |    1 |    0 |    1 |    1 |   7 |
| 2013 |    0 |    1 |    0 |    0 |   3 |
| 2014 |    1 |    0 |    1 |    1 |   2 |
| 2012 |    0 |    1 |    0 |    0 |  10 |
| 2014 |    0 |    0 |    1 |    0 |  12 |
| 2014 |    0 |    0 |    0 |    0 |   5 |
run;

data have_row_id / view=have_row_id;
  set have;
  rowid+1;
run;

proc transpose data=have_row_id out=have_categorical;
  by rowid yr pqr;
run;

proc tabulate data=have_categorical;
  class yr _name_;
  var col1;
  weight pqr;
  table _name_='', col1='' * sum=''*f=8. * (all='Total' yr='') / nocellmerge;
run;

=''删除标签单元并压缩输出。

Proc TABULATE output