New Contributor
Posts: 2

# WEIGHT calculation

Hi,

In order to measure performance and whether we can reduce any execution time, I am trying to convert some of SAS code into SQL query. While doing it, I came across a piece of code weight is being calculated. Need to know how SAS calculates weight for any given column so that I can try and replicate the same using SQL.

proc means data=input_file noprint;

var C_A/weight=T_A;

by period;

output out=output_file

mean=

Any insight on this would be of great help to me.

Thanks,

ankur

Respected Advisor
Posts: 4,736

## Re: WEIGHT calculation

The formulas used are  - if not obvious - normally documented as part of the documentation for the procedure.

Not sure if changing everything to SQL will help you a lot unless the data tables are stored in a data base. If stored in a data base: You might want to investigate the in-database capabilities of some of the PROCs (eg. Proc Means).

The quickest way of improving performance is in my experience to reduce passes through the data, reduction of volumes as fast as possible, re-design lookups (eg. by using hash tables).

New Contributor
Posts: 2

## Re: WEIGHT calculation

Patrick, Appreciate your response and I do agree with the Performance improvement point.

But as per the assignment, I need some way to do it in SQL. If some one could provide a sample input/output with the weight function, then I can re-engineer it to see how its calculated.

Thanks & Best Regards,

Ankur Vatsal

Occasional Contributor
Posts: 15

## Re: WEIGHT calculation

It would take two passes through the data to get the same result in SQL, but the final formula would be:

C_A*weight/sum_of_weights as weighted_mean

where the sum of weights would need to be calculated for each period.  You would need to join the summed weights back to the original data.

Occasional Contributor
Posts: 15

## Re: WEIGHT calculation

Posted in reply to MarkFisher

I clicked too soon--the weighted mean for each period would be the sum of C_A*weight divided by the sum of the weights, for each period.

Super User
Posts: 23,771

## Re: WEIGHT calculation

It's a weighted average

Weighted arithmetic mean - Wikipedia, the free encyclopedia

Additionally you need to account for missing and negative weights separately

 Weight value... PROC MEANS... 0 counts the observation in the total number of observations less than 0 converts the value to zero and counts the observation in the total number of observations missing excludes the observation
Discussion stats
• 5 replies
• 406 views
• 0 likes
• 4 in conversation