Hello Experts,
I have a dataset (see attached) with information for students in an examination.
I would like to calculate the weighted average of all levels for each unit by category and year
E.g
for Unit 1090, English, 2010
weight(level 1) =((Attended(Level 1)/Attended(1)+.............Attended(Level B2))>>>>>>for all levels
weighted_mean=sum(weight(Level 1)*mean_score(Level 1)..............weight(Level B2)*mean_score(Level B2))>>>>>>>for all levels
I tried to create macros for each variable required to compute the weighted mean but it takes forever to run.
What's the best way to do this? can I create all the macros in a single batch of codes? and how do I put the macros together to calculate the weighted mean.
%macro unit_loop(start,end);
proc sql;
%do unit=&start. %TO &end.;
create table exam.weighted&unit. as
select *from exam.scores where unit_code=&unit.;
%END
quit;
%mend;
%unit_loop(start=1090,end=401401)
Thank you,
Do you have the data at the student level, as in each thing graded/scored?
Likely a lot easier.
If multiple levels are needed you might consider Proc means/ summary with class statements.
You should have access to the SASHELP.Class data set so can run this code.
Proc summary data=sashelp.class; class sex age; var height weight; output out=work.summary mean= n= /autoname; run;
The output has summaries by combinations of the class variables as indicated by the variable _type_ in the output data set. _type_ = 0 is the summary across all records; 1 is the summary within the levels of age only, 2 is the summary within levels of sex only and 3 is the summary of combinations of sex and age.
We summarize two variables and the mean and count per _type_. The autoname option names the output variables: height_mean weight_mean height_n weight_n.
Which is one reason we say likely no macro is needed.
Starting with summarized data may be problematic. The proc means/summary has an optional FREQ statement that is used to indicate a variable that contains how many original records are represented. So I think that Freq attend; may be appropriate for this.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.
Find more tutorials on the SAS Users YouTube channel.