3.1 C
London
Friday, December 20, 2024
HomeSASDescriptive Statistics in SASHow to Calculate the Mean by Group in SAS

How to Calculate the Mean by Group in SAS

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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 print data=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;

The resulting table shows the mean of points scored by players based on their team and position.

Additional Resources

The following tutorials explain how to perform other common tasks in SAS:

How to Count Observations by Group in SAS
How to Calculate the Sum by Group in SAS
How to Create Frequency Tables in SAS

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories