Sample 25027: Compute the moving average of a variable
The sample code on the Full Code tab illustrates how to compute the moving average of a variable through an entire data set, over the last N observations in a data set, or over the last N observations within a BY-group.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
/* Create sample data */
data x;
do x=1 to 10;
output;
end;
run;
/* Sample 1: Compute the moving average of the entire data set */
data avg;
retain s 0;
set x;
s=s+x;
a=s/_n_;
run;
title 'Moving average of entire data set';
proc print;
run;
/* Sample 2: Compute the moving average of last 5 observations */
%let n = 5;
data avg;
retain s;
set x;
array qwe_(&n);
do i = &n to 2 by -1;
qwe_(i)=qwe_(i-1);
end;
qwe_(1)=x;
a = sum(of qwe_(*)) / &n;
retain qwe_: ;
drop qwe_: i ;
run;
title 'Moving average of last 5 observations';
proc print;
run;
/* Sample 3: Compute the moving average within a BY-Group of last n observations. */
/* For the first n-1 observations within the BY-Group, the moving average */
/* is set to missing. */
data ds1;
do patient='A','B','C';
do month=1 to 7;
num=int(ranuni(0)*10);
output;
end;
end;
run;
proc sort;
by patient;
run;
%let n = 4;
data ds2;
set ds1;
by patient;
retain num_sum 0;
if first.patient then do;
count=0;
num_sum=0;
end;
count+1;
last&n=lag&n(num);
if count gt &n then num_sum=sum(num_sum,num,-last&n);
else num_sum=sum(num_sum,num);
if count ge &n then mov_aver=num_sum/&n;
else mov_aver=.;
run;
title 'Moving average within BY-Group';
proc print;
run;
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
Sample 1
Moving average of entire data set
Obs s x a
1 1 1 1.0
2 3 2 1.5
3 6 3 2.0
4 10 4 2.5
5 15 5 3.0
6 21 6 3.5
7 28 7 4.0
8 36 8 4.5
9 45 9 5.0
10 55 10 5.5
Sample2
Moving average of last 5 observations
Obs x a
1 1 0.2
2 2 0.6
3 3 1.2
4 4 2.0
5 5 3.0
6 6 4.0
7 7 5.0
8 8 6.0
9 9 7.0
10 10 8.0
Sample 3
Moving average within BY-Group
Obs patient month num num_sum count last4 mov_aver
1 A 1 1 1 1 . .
2 A 2 4 5 2 . .
3 A 3 0 5 3 . .
4 A 4 4 9 4 . 2.25
5 A 5 0 8 5 1 2.00
6 A 6 8 12 6 4 3.00
7 A 7 3 15 7 0 3.75
8 B 1 8 8 1 4 .
9 B 2 9 17 2 0 .
10 B 3 7 24 3 8 .
11 B 4 3 27 4 3 6.75
12 B 5 9 28 5 8 7.00
13 B 6 5 24 6 9 6.00
14 B 7 2 19 7 7 4.75
15 C 1 4 4 1 3 .
16 C 2 4 8 2 9 .
17 C 3 0 8 3 5 .
18 C 4 5 13 4 2 3.25
19 C 5 5 14 5 4 3.50
20 C 6 9 19 6 4 4.75
21 C 7 9 28 7 0 7.00
Compute the moving average of a variable through an entire data set, over the last
N observations in a data set, or over the last
N observations within a BY-group.
Type: | Sample |
Topic: | SAS Reference ==> DATA Step SAS Reference ==> Functions
|
Date Modified: | 2010-08-25 13:35:02 |
Date Created: | 2005-01-13 15:36:23 |
Operating System and Release Information
SAS System | Base SAS | All | n/a | n/a |