我想计算adjust_volume变量,如下所示;
+------+------+---------------+-------------+-----------------+
| name | date | CountedVOLUME | DailyVolume | adjusted_volume |
+------+------+---------------+-------------+-----------------+
| A | 1 | 10 | | |
| A | 1 | 11 | 21 | |
| A | 2 | 12 | | |
| A | 2 | 13 | | |
| A | 2 | 14 | 39 | |
| A | 3 | 15 | 15 | |
| A | 4 | 16 | 16 | |
| A | 5 | 17 | | |
| A | 5 | 18 | | |
| A | 5 | 19 | | |
| A | 5 | 20 | 74 | |
| A | 6 | 21 | 0.127272727 | |
| A | 6 | 22 | 43 | 0.133333333 |
| B | 1 | 10 | | |
| B | 1 | 11 | 21 | |
| B | 2 | 12 | | |
| B | 2 | 13 | | |
| B | 2 | 14 | 39 | |
| B | 3 | 15 | 15 | |
| B | 4 | 16 | 16 | |
| B | 5 | 17 | | |
| B | 5 | 18 | | |
| B | 5 | 19 | | |
| B | 5 | 20 | 74 | |
| B | 6 | 21 | 0.127272727 | |
| B | 6 | 22 | 43 | 0.133333333 |
+------+------+---------------+-------------+-----------------+
Adjusted_volume = CountedVOLUME /(之前五天每日体积的总和)
为了简单起见,我为A和B使用相同的数字。
我该怎么做?
答案 0 :(得分:1)
Proc expand是您正在寻找的。工作的例子几乎可行。我相信你可以修改计算,以获得你正在寻找的东西。
data begin;
input name $ date CountedVOLUME @@;
cards;
A 1 10 A 1 11 A 2 12 A 2 13 A 2 14
A 3 15 A 4 16 A 5 17 A 5 18 A 5 19
A 5 20 A 6 21 A 6 22
B 1 10 B 1 11 B 2 12 B 2 13 B 2 14
B 3 15 B 4 16 B 5 17 B 5 18 B 5 19
B 5 20 B 6 21 B 6 22
;
run;
proc sort data=begin; by name date;
proc means data=begin noprint;
by name date;
output out= daily
vsum(CountedVOLUME) = dailyVolume
;
run;
proc expand data=daily out=expanded;
by name;
convert dailyVolume=movsum / transformout=(movsum 5);
run;
proc sort data = begin; by name date; run;
proc sort data = expanded; by name date; run;
data almost_wanted;
merge begin(in=a) expanded(in=b);
by name date;
if a and b;
run;
data wanted;
set almost_wanted;
adjusted_volume = CountedVOLUME / movsum;
run;
答案 1 :(得分:0)
这样可以准确生成您列出的样本结果。既然您说要计算adjusted_volume
,我认为dailyVolume
已存在于您的基础数据集中。
data have;
infile datalines dlm='|' missover;
input name $ date countedvolume dailyvolume;
datalines;
A|1|10
A|1|11|21
A|2|12
A|2|13
A|2|14|39
A|3|15|15
A|4|16|16
A|5|17
A|5|18
A|5|19
A|5|20|74
A|6|21|
A|6|22|43
B|1|10
B|1|11|21
B|2|12
B|2|13
B|2|14|39
B|3|15|15
B|4|16|16
B|5|17
B|5|18
B|5|19
B|5|20|74
B|6|21|
B|6|22|43
;
run;
data want (drop=cnt dv_:);
set have;
by name date;
retain dv_1 dv_2 dv_3 dv_4 cnt 0 dv_sum;
if first.name then do;
cnt=0;
dv_1=0;
dv_2=0;
dv_3=0;
dv_4=0;
dv_sum=0;
end;
adjusted_volume=countedvolume/dv_sum;
if last.date then do;
cnt=cnt+1;
if cnt=1 then dv_4=dailyvolume;
else if cnt=2 then dv_3=dailyvolume;
else if cnt=3 then dv_2=dailyvolume;
else if cnt=4 then dv_1=dailyvolume;
else do;
dv_sum=dailyvolume+dv_1+dv_2+dv_3+dv_4;
dv_4=dv_3;
dv_3=dv_2;
dv_2=dv_1;
dv_1=dailyvolume;
end;
end;
run;