Weights | Rating_a |
2 | 3 |
5 | 3 |
1 | 4 |
3 | 1 |
4 | 2 |
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;
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;
Just add an output statement as documented at https://documentation.sas.com/doc/de/pgmsascdc/9.4_3.5/proc/p04vbvpcjg2vrjn1v8wyf0daypfi.htm
@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)
@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.
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!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: