基于分组的总和值

时间:2015-04-29 19:29:09

标签: sas

考虑以下数据集 test

 Drug  Quantity State Year
  A     
  B
  C
  .        .      .     .

我如何总结按州和年分组的每种药物的数量?它会是这样的:

 data test;
 by Drug State Year;
 Total = sum(Quantity)
 run;

1 个答案:

答案 0 :(得分:1)

Mucio答案很好,但是如果您使用的是SAS SQL版本,那么它就是:

data test;
    input Drug $ Quantity State $ Year;
    datalines;
    A 10 NY 2013
    A 20 NY 2014
    B 110 NY 2013
    B 210 NY 2014   
    A 50 OH 2013
    A 60 OH 2014
    B 150 OH 2013
    B 260 OH 2014       
    A 22 NY 2014
    B 100 OH 2013
    ;
RUN;

PROC SQL;
   CREATE TABLE EGTASK.QUERY_FOR_TEST AS 
   SELECT t1.Drug, 
          t1.State, 
          t1.Year, 
          /* SUM_of_Quantity */
            (SUM(t1.Quantity)) AS SUM_of_Quantity
      FROM WORK.TEST t1
      GROUP BY t1.Drug,
               t1.State,
               t1.Year;
QUIT;

结果:

Result