Table of Contents
Calculating the mean by group in SAS refers to the process of finding the average value of a variable for different groups within a dataset using the SAS programming language. This is accomplished by first organizing the data into groups based on a specific variable, and then using the built-in function MEANS to calculate the mean for each group. The MEANS function calculates the sum of the values within each group and divides it by the total number of observations in that group, providing an overall average value for that group. This method is commonly used in statistical analysis to summarize and compare data across different groups and can be easily implemented in SAS through simple coding techniques.
Calculate the Mean by Group in SAS
You can use the following methods to calculate the mean of values by group in SAS:
Method 1: Calculate Mean by One Group
proc sql;
select var1, mean(var2) as mean_var2
from my_data
group by var1;
quit;
Method 2: Calculate Mean by Multiple Groups
proc sql;
select var1, var2, mean(var3) as mean_var3
from my_data
group by var1, var2;
quit;The following examples show how to use each method with the following dataset in SAS:
/*create dataset*/
data my_data;
input team $ position $ points;
datalines;
A Guard 15
A Guard 12
A Guard 29
A Forward 13
A Forward 9
A Forward 16
B Guard 25
B Guard 20
B Guard 34
B Forward 19
B Forward 3
B Forward 8
;
run;
/*view dataset*/
proc printdata=my_data;
Example 1: Calculate Mean by One Group
The following code shows how to calculate the mean of points by team:
/*calculate mean of points by team*/
proc sql;
select team, mean(points) as mean_points
from my_data
group by team;
quit;

From the output we can see that players on team A scored a mean of 15.66667 points and players on team B scored a mean of 18.16667 points.
Example 2: Calculate Mean by Multiple Groups
The following code shows how to calculate the mean of points, group by team and position:
/*calculate mean of points, grouped by team and position*/
proc sql;
select team, position, mean(points) as mean_points
from my_data
group by team, position;
quit;
Additional Resources
The following tutorials explain how to perform other common tasks in SAS:
Cite this article
stats writer (2024). How do you calculate the mean by group in SAS?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-do-you-calculate-the-mean-by-group-in-sas/
stats writer. "How do you calculate the mean by group in SAS?." PSYCHOLOGICAL SCALES, 1 Jul. 2024, https://scales.arabpsychology.com/stats/how-do-you-calculate-the-mean-by-group-in-sas/.
stats writer. "How do you calculate the mean by group in SAS?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-do-you-calculate-the-mean-by-group-in-sas/.
stats writer (2024) 'How do you calculate the mean by group in SAS?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-do-you-calculate-the-mean-by-group-in-sas/.
[1] stats writer, "How do you calculate the mean by group in SAS?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.
stats writer. How do you calculate the mean by group in SAS?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
