i**********a 发帖数: 32 | 1 大家好,被一道SAS操作问题困扰了很久,求问各路大神。
现有的dataset如下(数据经过简化):已有三个variable: state, quarter,size. 每
一个
state有4个quarter,每一个quarter又分别有3个observation. 我想对每一个state的每
一个
quarter求cumulative_size. 第四列显示的数据是我想要的结果,但是我不知道怎么写
SAS code来实现这个结果。由于实际的dataset 有51个state,这里只列了2个state,所
以不能用繁琐的if...then...来解决这个问题。大神们,求支招!!大谢!!!
state quarter size cumulative_size
Alaska 1 1 1
Alaska 1 2 3
Alaska 1 3 6
Alaska 2 1 1
Alaska 2 2 3
Alaska 2 3 6
Alaska 3 1 1
Alaska 3 2 3
Alaska 3 3 6
Alaska 4 1 1
Alaska 4 2 3
Alaska 4 3 6
Virginia 1 1 1
Virginia 1 2 3
Virginia 1 3 6
Virginia 2 1 1
Virginia 2 2 3
Virginia 2 3 6
Virginia 3 1 1
Virginia 3 2 3
Virginia 3 3 6
Virginia 4 1 1
Virginia 4 2 3
Virginia 4 3 6 |
C******t 发帖数: 72 | 2 I don't have SAS installed in my PC and can't be 100% sure it works.
proc sort data=youdata; by state quarter size;run;
data cumdata;
set youdata;
retain cumulative_size 0;
by state quarter;
if first.quarter then cumulative_size=size;
else cumulative_size=cumulative_size+size;
run; |
i**********a 发帖数: 32 | 3 谢谢解答!
【在 C******t 的大作中提到】 : I don't have SAS installed in my PC and can't be 100% sure it works. : proc sort data=youdata; by state quarter size;run; : data cumdata; : set youdata; : retain cumulative_size 0; : by state quarter; : if first.quarter then cumulative_size=size; : else cumulative_size=cumulative_size+size; : run;
|
s******8 发帖数: 102 | 4 防止有缺失值,用Sum();
data cumdata;
set youdata;
retain cumulative_size 0;
by state quarter;
if first.quarter then cumulative_size=size;
else cumulative_size=sum(cumulative_size,size);
run; |