Partitioned Running Totals in SAS

后端 未结 4 450
忘掉有多难
忘掉有多难 2021-01-24 17:51

How can I create a new column for an existing dataset that is the running total of an existing column - partitioned by some identifier?

ID |  Value |     New Val         


        
4条回答
  •  执念已碎
    2021-01-24 18:50

    In the data step this is accomplished with the sum statement.

    data want;
      set have;
      by id;
      if first.id then running_total=0;
      running_Total + value;
    run;
    

    In PROC SQL this would be impossible unless you have an ordering variable (in which you could do something like this):

    proc sql;
      create table want as
       select id, value,
         (select sum(value) from have V
         where V.id=H.id and V.ordervar le H.ordervar
         ) as running_total
       from have H
     ;
    quit;
    

    But SAS doesn't have a partition by concept - the SAS data step is far more powerful than that.

提交回复
热议问题