## Weighted average

Solved
Frequent Contributor
Posts: 89

# Weighted average

Dear all,

For variable X, I try to calculate min, max and mean with one statement in PROC TABULATE.

Min and max is no problem.

For mean however, I need a weighted average.

This is the sum of variable X, divided not by n, but with the sum of the weight variable Y.

How can this be done (not with data step programming or proc report stuff)?

I tried several options without success (weight statement, DF option, ...)

GreetZ,

Herman

Accepted Solutions
Solution
‎10-05-2011 07:14 AM
PROC Star
Posts: 8,163

## Weighted average

I think you may misunderstand the effect of a weight statement as I believe that it always affects the numerator.  You can use vardef to determine the effect on the denominator.  Take a look at:

http://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/viewer.htm#a002473744.htm

All Replies
Solution
‎10-05-2011 07:14 AM
PROC Star
Posts: 8,163

## Weighted average

I think you may misunderstand the effect of a weight statement as I believe that it always affects the numerator.  You can use vardef to determine the effect on the denominator.  Take a look at:

http://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/viewer.htm#a002473744.htm

Super User
Posts: 10,761

## Weighted average

As far as I know ,It looks like proc tabulate can not calculate weighted mean.Maybe you should use data step or SQL to get it.

Ksharp

PROC Star
Posts: 8,163

## Weighted average

Ksharp,

Why do you say that?  Try the following:

data test;

set sashelp.class;

if sex eq "F" then impact=2;

else impact=1;

run;

PROC TABULATE data=test;

CLASS sex;

VAR height;

TABLE height*MEAN, sex ALL;

RUN;

PROC TABULATE data=test;

CLASS sex;

VAR height;

weight impact;

TABLE height*MEAN, sex ALL;

RUN;

Super User
Posts: 10,761

## Weighted average

Art. You are right. I overlooked it.

Super User
Posts: 3,909

## Weighted average

You can also do weighted averages in PROC MEANS and PROC REPORT. This may be useful if you wish to confirm the calculations by another independent method.

PROC Star
Posts: 8,163

## Weighted average

I agree with verifying the results, but I think what the OP originally indicated wanting was something like:

data test;

set sashelp.class;

if sex eq "F" then impact=2;

else impact=1;

new_height=height/impact;

run;

PROC TABULATE data=test;

CLASS sex;

VAR new_height;

weight impact;

TABLE new_height*MEAN, sex ALL;

RUN;

🔒 This topic is solved and locked.