Hello,
Can anyone tell me how to calculate weighted averages in this example:
data premium; input id fraction premium; datalines; 1 1 1000 2 0.5 500
run;
Customers 1 and 2 both payed the same premium, but no. 2 was insured for only six months.
They the same annual premium, so in my opinion the average premium per person is 1000.
However, using weight does not work correctly in this case:
proc means mean; weight fraction; var premium; run;
It returns 833 as the average premium.
Currently I solve this by exporting means output, summing fraction and premium and divide.
Does anyone know of an easy, better way - without calculating new variables in the datastep (there are many variables)?
Thanks,
Eric
Chris was faster and is correct.
What you want is the weighted mean of the annualized premiums with the fractions being the weights. This, indeed, amounts to (sum of premiums)/(sum of fractions) and hence can be calculated as shown by Chris or similarly as follows:
proc sql;
select sum(premium)/sum(fraction) as mean_premium
from premium;
quit;
You could use PROC SQL:
proc sql;
select mean(premium/fraction) as mean_premium
from premium;
quit;
Thanks for asking your question on the communities here. I see you've also asked it in other places (SAS-L, blog comments) so I want to add some more of that context.
It seems like you really want an "annualized premium" -- what each customer is paying per annum (year) as a rate. If the term is something different that 1.0 years, you need a formula to calculate what the annual rate is.
I've seen this answer on SAS-L by "retired mainframer":
data null;
set premium end=eof;
do until (eof);
sum+(premium/fraction);
count+1;
end;
average = sum/count;
put average=;
run;
The SQL approach by @FreelanceReinh might also serve your purpose.
Thanks Chris and Reinhard,
The next step is that I want the premiumpaid by No 2 to weigh less than the premium paid by No 1.
If I change the case to
Id 1, fraction=1, premium=900
Id 2, fraction=0.5, premium=600
The average annual premium should be (900 + 600) / 1.5 = 1000.
In Reinhard's case it is (900 + 1200) / 2 = 1050.
I can see it can be solved using Chris' script. But then I have dozens of variables I want to treat this way...
But thanks!! for your quick replies.
Cheers,
Eric
If you prefer SQL, I think this accomplishes something similar:
PROC SQL;
CREATE TABLE WORK.Avgs AS
SELECT (COUNT(t1.id)) AS N,
(SUM(t1.fraction)) AS AnnumTotal,
(SUM(t1.premium)) AS PremiumTotal,
/* AnnualizedAvg */
calculated PremiumTotal / calculated AnnumTotal AS AnnualizedAvg
FROM WORK.PREMIUM t1;
QUIT;
Chris was faster and is correct.
What you want is the weighted mean of the annualized premiums with the fractions being the weights. This, indeed, amounts to (sum of premiums)/(sum of fractions) and hence can be calculated as shown by Chris or similarly as follows:
proc sql;
select sum(premium)/sum(fraction) as mean_premium
from premium;
quit;
No, wait a minute! You don't need a macro. I can prepare an array solution for you.
So, you have more variables like PREMIUM, let's say three, for example: PREMIUM, VAR2, VAR3. And let's assume for a moment that the weights ("fractions") are the same for all these variables. (Or do you have individual fractions for each of them?)
In this case you could extend the DOW loop approach cited by Chris. But I'm afraid there is a correction necessary: The SET statement must be moved inside the DO-UNTIL loop (that's the trick of the DOW loop after all), otherwise we have an infinite loop.
Now you can calculate the target variables, let's call them MEAN_PREMIUM, MEAN_VAR2 and MEAN_VAR3, as follows:
data premium1;
input id fraction premium var2 var3;
cards;
1 1 1000 900 500
2 0.5 500 600 400
;
%let n=3; /* number of variables like PREMIUM */
data _null_;
array p[&n] premium var2 var3;
array s[&n];
array avg[&n] mean_premium mean_var2 mean_var3;
do until (eof);
set premium1 end=eof;
do i=1 to &n;
s[i]+p[i];
end;
wgt+fraction;
end;
do i=1 to &n;
avg[i] = s[i]/wgt;
end;
put avg(*)=;
run;
If you need individual fractions for each of the three variables, we could introduce a fourth array, which would be no problem at all.
Perhaps I am not understanding the problem, but I have to disagree with several of the submitted "solutions." The solution that you accepted as correct does not seem correct to me. Both @ChrisHemedinger and @FreelanceReinh suggested implementations of the formula sum(premium)/sum(fraction). That is not the same as mean(premium/fraction), which I think is the correct formula.
Hello (again) Rick,
In my data I have premiums and fractions of a year that a customer was insured.
So in my opinion the average premium per customer is sum(premium)/sum(fraction). This is a sort of weighted average, unfortunately not supported by the MEANS procedure.
So being the customer (who is always right ), I think the solutions are correct.
Thanks for your thoughts!
Cheers, Eric
There is a similar question, marked as solved, but I cannot find the solution.
https://communities.sas.com/t5/SAS-Procedures/Weighted-average/m-p/24378#U24378
In the solution is referred to the VARDEF option, but that does not apply to the weighted mean calculation.
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.