# 如何使用分组依据来计算均值差

##### 问题描述投票：0回答：2

``````id  2015  2016  2017
1   £10   £12   £11
2   £12   £14   £13
3   £11   £20   £10
4   £10   £13   £21
5   £15   £11   £18
``````

``````id  2015  2016  2017
1   £10   £12   £11
2   £12   £14   £13
3   £11   £20   £10
4   £10   £13   £21
5   £15   £11   £18
Mean  £11.6   £14   £14.6
``````

``````proc means data=work.dataset1;
output out=work.dataout mean= /autoname;
var amt;
class id;
run;
``````

``````mean_diff_12= mean - mean2
mean_diff_23= mean2 - mean3
``````

``````mean2=lag1(mean)
mean3=lag2(mean)
``````

sas
##### 2个回答
1

``````data have; input
id amt2015-amt2017; datalines;
1   10   12   11
2   12   14   13
3   11   20   10
4   10   13   21
5   15   11   18
run;

proc means noprint data=have;
var amt2015-amt2017;
output out=means mean=amt2015-amt2017 / autoname;
run;

data means;
set means;
meandiff_1_2 = amt2015-amt2016;
meandiff_2_3 = amt2016-amt2017;
run;

proc print data=means; run;

-------- listing --------
meandiff_    meandiff_
Obs    _TYPE_    _FREQ_    amt2015    amt2016    amt2017       1_2          2_3

1        0         5        11.6        14        14.6        -2.4         -0.6
``````

1

``````data have ;
input id \$ @;
do year=2015 to 2017 ;
input amt @ ;
output;
end;
cards;
1  10 12 11
2  12 14 13
3  11 20 10
4  10 13 21
5  15 11 18
;
```然后您可以使用PROC MEANS查找每个时间段的平均amt。```
``````proc summary data=have nway ;
class year;
var amt ;
output out=means mean=mean_amt ;
run;
```以及一个简单的数据步骤来计算时间段之间的差：```
``````data want;
set means;
mean_dif = dif(mean_amt);
run;
```结果：```
``````Obs    year    _TYPE_    _FREQ_    mean_amt    mean_dif

1     2015       1         5        11.6          .
2     2016       1         5        14.0         2.4
3     2017       1         5        14.6         0.6
``````