Table of Contents
A moving average in SAS is a statistical technique used to analyze data over a specific period of time by smoothing out fluctuations and identifying trends. To calculate a moving average in SAS, first, the data must be arranged in a time series format with equal intervals. Then, the moving average can be calculated by taking the average of a specified number of data points, also known as the window size, and moving it over the time series. This process is repeated for each data point, resulting in a series of averages that can be used for further analysis. SAS provides various functions and procedures such as PROC EXPAND and PROC MEANS that can be used to easily calculate moving averages.
Calculate a Moving Average in SAS
In statistics, a moving average represents the average of the previous n values in a dataset.
The easiest way to calculate a moving average in SAS is to use the proc expand statement.
The following example shows how to use this statement in practice.
Example: Calculate a Moving Average in SAS
Suppose we create the following dataset in SAS:
/*create dataset*/ data original_data; input time values; datalines; 1 7 2 12 3 14 4 12 5 16 6 18 7 11 8 10 9 14 10 17 ; run;/*view dataset*/ proc printdata=original_data;

Now suppose we would like to calculate a 3-period moving average for the values column.
We can use proc expand to do so:
/*calculate 3-period moving average for values*/
proc expanddata=original_data out=out_data method=none;
id time;
convert values = values_ma3 / transout=(movave 3);
run;
/*view results*/
proc printdata=out_data;
The new column called values_ma3 displays the 3-period moving average for the values column.
For example, the third value in the values_ma3 column represents the average of the previous 3 periods:
- Moving Average = (7+12+14) / 3 = 11.0000
The fourth value in the values_ma3 column represents the average of the previous 3 periods as well:
- Moving Average = (12+14+12) / 3 = 12.6667
And so on.
For example, we could use the following code to calculate a 4-period moving average for the values column:
/*calculate 4-period moving average for values*/
proc expanddata=original_data out=out_data method=none;
id time;
convert values = values_ma4 / transout=(movave 4);
run;
/*view results*/
proc printdata=out_data;
The new column called values_ma4 displays the 4-period moving average for the values column.
The following articles explain how to perform other common tasks in SAS:
Cite this article
stats writer (2024). How do I calculate a moving average in SAS?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-do-i-calculate-a-moving-average-in-sas/
stats writer. "How do I calculate a moving average in SAS?." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/how-do-i-calculate-a-moving-average-in-sas/.
stats writer. "How do I calculate a moving average in SAS?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-do-i-calculate-a-moving-average-in-sas/.
stats writer (2024) 'How do I calculate a moving average in SAS?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-do-i-calculate-a-moving-average-in-sas/.
[1] stats writer, "How do I calculate a moving average in SAS?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How do I calculate a moving average in SAS?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
