【发布时间】:2018-11-23 12:07:11
【问题描述】:
我有这张桌子test
test<-matrix(c(1,1,1,1,1,2,2,2,2,2,2011,2012,2012,2013,2014,2011,2013,2013,2014,2014,1,1,3,2,1,2,1,1,3,1), 10,3)
test<-as_data_frame(test)
colnames(test)<-c("T","Y","S")
我想创建一个变量x,它是变量S 的总和,其中年份Y 与该行或一年前的行相同。
这正是我所期待的:
test<-cbind(test,c(1,5,5,6,3,2,4,4,6,6))
colnames(test)[4]<-"x"
我认为在 SQL 中是这样的(至少我记得):
proc sql;
create table test as select
a.T,
a.Y,
sum(case when Y eq a.Y or Y eq a.Y+1 then S else 0 end) as x
from test as a
group by T, Y;
end;
【问题讨论】: