本文介绍了Proc Sql:运行总计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据,我正在创建运行总计 -

I have below data and I'm creating running total -

data test;
input id$ year qtr sales;
cards;
a 2015 1 100
a 2015 2 2200
a 2015 3 100
a 2015 4 100
b 2015 1 10
b 2015 2 10


;run;

proc sql;
select a.id,a.year,a.qtr,a.sales,sum(a.sales) as running_tot
from test a,test b where a.id=b.id and a.year=b.year and a.qtr >= b.qtr
group by 1,2,3,4;
quit;

但结果不正确,但如果我将销售额设为 100 而不是 2200,结果是否正确?

But the result is not correct, but if I make sales 100 instead of 2200, result is coming correct?

推荐答案

您对 SALES 变量的错误副本求和.

You are summing the wrong copy of the SALES variable.

select a.id,a.year,a.qtr,a.sales
     , sum(b.sales) as running_tot
  from test a
  left join test b on a.id=b.id and a.year=b.year and a.qtr >= b.qtr
  group by 1,2,3,4
;

当然,只使用数据步骤更容易.

Of course it is easier to just use a data step.

data want ;
  set test;
  by id year qtr;
  if first.year then running_tot=0;
  running_tot + sales;
run;

这篇关于Proc Sql:运行总计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 03:13