BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
SASuser4321
Obsidian | Level 7
I have a table with two columns and I wish to find the weighted mean.
So for example, in the following table, I would like my code to generate the following results:  2.4
I got the above value of 2.4 through this:([(3*2) + (3*5) + (4*1) + (1*3) + (2*4)]/15
My actual table has thousands of rows and so I'm wondering what code could deal with this. Help would be appreciated. 
I tried the following code but it doesn't work:
proc sql;
select sum(Rating_a*Weights) / sum(Weights) from have;
quit;
Weights Rating_a
2 3
5 3
1 4
3 1
4 2
1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

Maxim 7 from Maxims of Maximally Efficient SAS Programmers  says: There is a procedure for it.

 

So instead of using proc sql, use proc means:

proc means data=have mean;
   var Rating_a;
   weight Weight;
run;

View solution in original post

6 REPLIES 6
andreas_lds
Jade | Level 19

Maxim 7 from Maxims of Maximally Efficient SAS Programmers  says: There is a procedure for it.

 

So instead of using proc sql, use proc means:

proc means data=have mean;
   var Rating_a;
   weight Weight;
run;
SASuser4321
Obsidian | Level 7
how do I show this result separately as a table in the "Output Data" section? Because I would like to merge this table with other data sets eventually. I tried the following but it doesn't work:

proc means data=have mean;
output out = weighted;
var Rating_a;
weight Weight;
run;
SASuser4321
Obsidian | Level 7
I figured it out on my own 🙂


ods select none;
proc means data=have mean;
var Rating_a;
weight Weight;
ods output summary= weighted;
run;

ods select all;
PaigeMiller
Diamond | Level 26

@SASuser4321 wrote:
how do I show this result separately as a table in the "Output Data" section? Because I would like to merge this table with other data sets eventually. I tried the following but it doesn't work:

proc means data=have mean;
output out = weighted;
var Rating_a;
weight Weight;
run;

What about it doesn't work? Please provide more information. (IMPORTANT: when you tell us something doesn't work and give no other information, we can't help you. Always tell us what happened and explain waht part didn't work, or show us the log with errors)

--
Paige Miller
PaigeMiller
Diamond | Level 26

@SASuser4321 wrote:
I have a table with two columns and I wish to find the weighted mean.
So for example, in the following table, I would like my code to generate the following results:  2.4
I got the above value of 2.4 through this:([(3*2) + (3*5) + (4*1) + (1*3) + (2*4)]/15
My actual table has thousands of rows and so I'm wondering what code could deal with this. Help would be appreciated. 
I tried the following code but it doesn't work:
proc sql;
select sum(Rating_a*Weights) / sum(Weights) from have;
quit;

This formula will fail and give you the wrong answer if you have missing values. Do not write your own code to compute means or other basic statistics, do not write your own code to compute anything is SAS has already programmed it. Instead, as suggested use PROC MEANS which has been thoroughly debugged and tested in a bazillion and 12 different applications.

--
Paige Miller

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 488 views
  • 2 likes
  • 3 in conversation