I have data sorted into 100 lb weight groups. Within each group I would like to compute a weighted average price. The formula is
(sum of (Lot weight x price)) / sum of Lot weight
How do I compute this value for each weight group. Thank you.
PROC MEANS will do this, use the WEIGHT variable to specify the weights and CLASS to specify the groupings.
If you need more help post your code.
@Mike15 wrote:
I have data sorted into 100 lb weight groups. Within each group I would like to compute a weighted average price. The formula is
(sum of (Lot weight x price)) / sum of Lot weight
How do I compute this value for each weight group. Thank you.
Did you run the code? Which parts don't you understand?
PROC MEANS prepares summary statistics, so it will summarize your data and apply the weights to the summary.
Regarding your code (data feedercombined_1) you can simplify it using IN. These two lines will replace your block of IF regarding color and sex.
if color in (5:8) then delete;
if sex in (2:9) then delete;
Or replace both with:
if color in (5:8)
OR sex in (2:9)
OR PreCond=0 then delete;
That's happening in the next step which doesn't change anything in the code I suggested. The error here is in the logic not aligning with the data or your expectations of whats in the data, not the code. The code is syntaxtically correct.
I would start by removing the statements in the data step that creates FeederCombined_2 and add them back one by one to see which one is causing the issue or unexpected behaviour.
Also, this can't be correct:
proc means data=feedercombined_2;
class avgwgt; <- this is your grouping variable, ie I want analysis by each sex or region;
var price;
weight avgwgt; <- this is the weight variable, it should not be the same as the CLASS variable;
output out = want mean= weighted_avg;
run;
Just to confirm. The following code creates a weighted average within each weight class. This is computed as ?(lot wgt x price) ÷ ?(lot wgt)?
I don't think that's correct. Please see my comments in my previous post.
Your proc means is incorrect, it doesn't make sense to have the CLASS and WEIGHT be the same. Maybe the third times the charm.
This may be what you want:
proc means data=testfall2016_2;
class Wgt300399;
var price;
weight avgwgt;
output out = want mean= weighted_avg;
run;
Then you need to post sample data now.
It does not need to be your real data, but the structure and type should be the same.
@Mike15 wrote:
No, sorry. Still the straight mean.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.