How do you calculate a weighted average in SAS? Can you provide some examples? 2

How do you calculate a weighted average in SAS? Can you provide some examples?

Weighted average in SAS can be calculated using the PROC MEANS procedure. This procedure allows us to calculate the weighted average by specifying the variable to be averaged and the weight variable. The weight variable assigns different weights to each observation based on its importance. For example, in a survey, the weight variable may represent the population size of each group.

An example of calculating the weighted average in SAS would be finding the average test scores of a class where each student’s score is weighted by their attendance. The PROC MEANS procedure would be used with the test scores as the variable to be averaged and the attendance as the weight variable.

Another example could be finding the average sales of a company where the sales from each region are weighted by their contribution to the overall revenue. In this case, the PROC MEANS procedure would be used with the sales as the variable to be averaged and the revenue contribution as the weight variable.

In conclusion, weighted average in SAS can be calculated using the PROC MEANS procedure by specifying the variable to be averaged and the weight variable. This allows for more accurate and meaningful averages, taking into account the varying importance of each observation.

Calculate a Weighted Average in SAS (With Examples)


You can use the following methods to calculate a weighted average in SAS:

Method 1: Calculate Weighted Average

proc sql;
    createtable new_data asselectsum(weight * value) / sum(weight) as weighted_average
    from original_data;
quit;

Method 2: Calculate Weighted Average by Group

proc sql;
    createtable new_data asselect grouping_variable,
    sum(weight * value) / sum(weight) as weighted_average
    from original_data
    group by grouping_variable;
quit;

The following examples show how to use each method with the following dataset in SAS:

/*create dataset*/
data original_data;
    input sales_rep $ price amount;
    datalines;
A 8 1
A 5 3
A 6 2
B 7 2
B 12 5
B 14 4
;
run;

/*view dataset*/
proc printdata=original_data;

Example 1: Calculate Weighted Average

The following code shows how to calculate a weighted average for the price variable, using the amount variable as the weight:

/*calculate weighted average of price*/
proc sql;
    createtable new_data asselectsum(amount * price) / sum(amount) as weighted_average
    from original_data;
quit;

/*view weighted average of price*/
proc printdata=new_data;

The weighted average of price turns out to be 9.70588.

Example 2: Calculate Weighted Average by Group

The following code shows how to calculate the weighted average of the price variable, grouped by the sales_rep variable:

/*calculate weighted average of price, grouped by sales_rep*/
proc sql;
    createtable new_data asselect sales_rep,
    sum(amount * price) / sum(amount) as weighted_average
    from original_data
    group by sales_rep;
quit;

/*view results*/
proc printdata=new_data;

  • The weighted average of price for sales rep A is 5.8333.
  • The weighted average of price for sales rep B is 11.8182.

Additional Resources

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

Cite this article

stats writer (2024). How do you calculate a weighted average in SAS? Can you provide some examples?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-do-you-calculate-a-weighted-average-in-sas-can-you-provide-some-examples/

stats writer. "How do you calculate a weighted average in SAS? Can you provide some examples?." PSYCHOLOGICAL SCALES, 1 Jul. 2024, https://scales.arabpsychology.com/stats/how-do-you-calculate-a-weighted-average-in-sas-can-you-provide-some-examples/.

stats writer. "How do you calculate a weighted average in SAS? Can you provide some examples?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-do-you-calculate-a-weighted-average-in-sas-can-you-provide-some-examples/.

stats writer (2024) 'How do you calculate a weighted average in SAS? Can you provide some examples?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-do-you-calculate-a-weighted-average-in-sas-can-you-provide-some-examples/.

[1] stats writer, "How do you calculate a weighted average in SAS? Can you provide some examples?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.

stats writer. How do you calculate a weighted average in SAS? Can you provide some examples?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top