2017-08-15 68 views
1

我想計算adjusted_volume變量,如下所示;如何使用SAS計算分部?

+------+------+---------------+-------------+-----------------+ 
| 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 /(在求和前五天DailyVolumes的)

我使用相同的編號爲A和B只是爲了簡單起見。

我該怎麼做?

+0

https://stackoverflow.com/help/how-to-ask – Reeza

回答

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; 
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; 
+0

'PROC expand'是海報可以不具有SAS/ETS過程。 – user2877959

+0

@ user2877959好點。 – pinegulf