Table of Contents
The process of calculating a weighted average in SAS is a fundamental skill for data professionals, allowing for statistical measures that accurately reflect the relative importance of different observations. Unlike the simple arithmetic mean, which treats every data point equally, the weighted average accounts for varying influence by assigning a corresponding weight to each value. Mathematically, this calculation is achieved by multiplying the raw data values by their associated weights, summing these products, and finally dividing the total sum by the sum of all weights. This methodology ensures that high-impact observations exert greater influence on the final result.
The syntax required to perform this calculation in the SAS environment is clean and highly efficient, relying primarily on the powerful PROC SQL procedure. Whether you are manipulating data stored within a proprietary SAS dataset or importing data from an external source, the methods outlined below provide robust solutions for deriving both overall and group-specific weighted averages. We will explore two primary approaches, followed by practical examples demonstrating their implementation.
To accurately calculate a weighted average within SAS, we rely heavily on the aggregation capabilities found within PROC SQL. This procedure allows for the direct manipulation of the average formula using the SUM function, making it ideal for tasks that require complex aggregations that might be difficult to achieve solely with the traditional PROC MEANS or PROC UNIVARIATE procedures. The following sections detail the core syntax patterns used in these calculations.
Method 1: Calculating the Overall Weighted Average
This method focuses on calculating a single, overall weighted average for a specific variable across the entire dataset. This is analogous to finding the average price of an inventory item where the “weight” is the quantity sold, giving a true average cost that reflects transaction volume. The key is implementing the mathematical definition: SUM(value * weight) / SUM(weight). This powerful one-line calculation is performed within the SELECT statement of the PROC SQL procedure, ensuring maximum efficiency and readability.
The structure below demonstrates how to define the output table and apply the fundamental weighted average calculation across the source table named original_data. Note the use of the AS keyword to assign a descriptive name to the resulting calculated column, which is essential for clear reporting.
proc sql;
create table new_data as
select sum(weight * value) / sum(weight) as weighted_average
from original_data;
quit;
Method 2: Calculating Weighted Average by Group
Often, statistical analysis requires segmenting the data and calculating the weighted average for specific subgroups—such as departmental performance, regional sales figures, or demographic cohorts. To achieve this, we introduce the GROUP BY clause into the PROC SQL structure. The GROUP BY variable is included in the SELECT statement, and the aggregation function (the weighted average formula) is applied independently to each unique value within that grouping variable.
This approach is indispensable for generating reports that require granular, statistically sound metrics. For instance, if analyzing product quality data, a weighted average score might be required separately for products manufactured in Factory A versus Factory B. The following syntax utilizes the grouping_variable to partition the dataset before applying the weighted average formula, yielding multiple results—one for each group.
proc sql;
create table new_data as
select grouping_variable,
sum(weight * value) / sum(weight) as weighted_average
from original_data
group by grouping_variable;
quit;Setting Up the Sample Data in SAS
To fully illustrate these methods, we will apply the calculations to a sample dataset that simulates transaction records. This dataset, named original_data, includes three crucial variables: sales_rep (our grouping variable, which is character type denoted by the $), price (the variable whose average we want to calculate, our ‘value’), and amount (the quantity sold, which will serve as our ‘weight’). Defining this dataset first ensures that we have the necessary structure and values required for executing the subsequent PROC SQL statements.
The following code block uses the DATA step and DATALINES to create this sample data in memory. Subsequently, PROC PRINT is used to display the contents of the newly created SAS dataset, allowing for visual confirmation of the data structure before proceeding with the statistical analysis. Understanding the input data is the first step toward verifying the accuracy of the calculated weighted averages.
/*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 print data=original_data;
Example 1: Calculating the Overall Weighted Average
In this first practical example, our objective is to determine the overall average price of all transactions, where the relative importance of each price observation is determined by the amount sold. Therefore, the price variable serves as the value (X), and the amount variable serves as the weight (W). This method provides a single scalar value that represents the centralized tendency of the price, heavily influenced by sales volume. A price associated with an amount of 5 will influence the average five times more than a price associated with an amount of 1.
We use the core Method 1 syntax, substituting value with price and weight with amount. The resulting table, new_data, will contain only one row and one column: the calculated weighted average. This is a common requirement in aggregate reporting where a single metric must summarize the performance of the entire business unit or dataset.
/*calculate weighted average of price*/
proc sql;
create table new_data as
select sum(amount * price) / sum(amount) as weighted_average
from original_data;
quit;
/*view weighted average of price*/
proc print data=new_data;Executing the code yields the following output, confirming that the overall weighted average price, adjusted for the sales volume (amount), is approximately 9.71.

Upon review of the output, the weighted average of price turns out to be precisely 9.70588. This figure represents the average unit price across all transactions, factoring in the quantity sold for each price point. If we had calculated a simple arithmetic mean (unweighted), the result would be significantly different, highlighting the importance of using weights in transactional data.
Example 2: Calculating Weighted Average Grouped by Sales Representative
In a sales context, it is far more informative to assess performance metrics on a per-representative basis. Therefore, in this second example, we apply Method 2 to calculate the weighted average price separately for each unique value found in the sales_rep variable. We are still using price as the value and amount as the weight, but we introduce the GROUP BY sales_rep clause to partition the calculation. This ensures that the weighted average for Sales Rep A only considers A’s transactions, and Sales Rep B’s average only considers B’s transactions.
This grouped calculation provides actionable business intelligence, allowing managers to compare the average unit price achieved by different representatives. The resulting table will contain two rows—one for Sales Rep A and one for Sales Rep B—each showing their respective volume-adjusted average price. This segmentation is critical for diagnosing performance variations and identifying training needs or pricing inconsistencies across the team.
/*calculate weighted average of price, grouped by sales_rep*/
proc sql;
create table new_data as
select sales_rep,
sum(amount * price) / sum(amount) as weighted_average
from original_data
group by sales_rep;
quit;
/*view results*/
proc print data=new_data;
The execution of the grouped query generates a table that clearly separates the weighted average prices for the two sales representatives, offering a direct comparison of their respective sales efficiency. This detailed breakdown demonstrates the power of utilizing the GROUP BY clause within PROC SQL for complex analytical reporting.

The results provide two distinct weighted averages, allowing for immediate performance analysis:
- The weighted average of price for sales rep A is 5.8333. This lower average suggests Rep A either deals with lower-priced items or applies greater discounts.
- The weighted average of price for sales rep B is 11.8182. This significantly higher average suggests Rep B focuses on higher-value transactions or successfully commands higher prices for similar products.
Summary of Weighted Average Techniques in SAS
The calculation of weighted averages in SAS is most effectively managed through the PROC SQL procedure, which provides the necessary flexibility to implement the complex aggregation formula (SUM(X*W)/SUM(W)). By mastering the two methods outlined—the overall aggregation and the grouped aggregation—analysts can generate statistically sound metrics that accurately reflect the underlying data distribution and the true impact of different variables.
Whether you require a single global metric or detailed subgroup analysis, the consistency and power of the PROC SQL syntax ensure reliable and reproducible results. These techniques are highly transferable and form the basis for many advanced analytical operations in areas such as financial modeling, demographic studies, and quality control reporting.
The following tutorials explain how to perform other common tasks in SAS:
Cite this article
stats writer (2025). How to Easily Calculate Weighted Averages in SAS. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-to-calculate-a-weighted-average-in-sas-with-examples/
stats writer. "How to Easily Calculate Weighted Averages in SAS." PSYCHOLOGICAL SCALES, 1 Dec. 2025, https://scales.arabpsychology.com/stats/how-to-calculate-a-weighted-average-in-sas-with-examples/.
stats writer. "How to Easily Calculate Weighted Averages in SAS." PSYCHOLOGICAL SCALES, 2025. https://scales.arabpsychology.com/stats/how-to-calculate-a-weighted-average-in-sas-with-examples/.
stats writer (2025) 'How to Easily Calculate Weighted Averages in SAS', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-to-calculate-a-weighted-average-in-sas-with-examples/.
[1] stats writer, "How to Easily Calculate Weighted Averages in SAS," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, December, 2025.
stats writer. How to Easily Calculate Weighted Averages in SAS. PSYCHOLOGICAL SCALES. 2025;vol(issue):pages.