To calculate the mean of variable "value" with weight, we can simply do the following:
proc sql;
select sum(Value*Weight)/sum(Weight) as WeightedAverage
from table ;
quit;
How can we calculate median of the variable "value" with weight? I know we can use proc means, but I would prefer a solution in proc sql.
Example:
data table;
input value weight;
datalines;
1 1
2 1
3 2
;
run;
Using proc means, we can get the mean (2.25) and median (2.5) easily:
proc means data=table mean median;
var value;
weight weight;
run;
Using above proc sql, I can get the weighted average: 2.25.
How to get the median (which is 2.5)?