BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
PQK
Calcite | Level 5 PQK
Calcite | Level 5

Dear Forum,

I am trying to output the geometric and harmonic mean for one variable that is subsetted by two other variables. As a reference, I would like to calculate GEOMEAN and HARMEAN just as PROC MEANS would do in the following example.

proc means data=inputdata noprint maxdec=2;

var var3;

class var1 var2;

output out=outputdata Mean(var3)=Mean;

run;

PROC MEANS accomplishes this task easily for the arithmetic mean, but I am stuck trying to find a work around for geometric and harmonic mean…sigh.

Any suggestions would be greatly appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Actually, Proc SQL is much clearer...

proc sql;

    create table summary as

    select sex, mean(height) as avg_height, count(height)/sum(1/height) as harm_mean, exp(mean(log(height))) as geom_mean

    from sashelp.class

    group by sex;

quit;

View solution in original post

4 REPLIES 4
Ksharp
Super User

I checked the documentation. there are not such options.

But at Function, I found these two functions to calculate these means.

data mean;
 set sashelp.class;
 geomean=geomean(age,weight,height);
 harmean=harmean(age,weight,height);
run;




Ksharp

Reeza
Super User

You can use proc transpose and the functions that KSharp has suggested, or use some transformations with proc means. I prefer the transformations as its easier for me to understand.

For geometric mean take the log of your variable and then exponentiate the resulting mean of the logged variable.

For harmonic mean take the inverse of your variable and then n/sum(inverse) is the number you're looking for.

data class;

    set sashelp.class;

    harm=1/height;

    geom=log(height);

run;

ods table summary=step1;

proc means data=class n mean sum;

class sex;

var height harm geom;

run;

data step2;

    set step1;

    geometric_mean=exp(geom_mean);

    harmonic_mean=harm_n/harm_sum;

    keep sex height_mean geometric_mean harmonic_mean;

run;

You can verify it using KSharps formulas:

proc sort data=class;

by sex;

run;

proc transpose data=class out=class2;

by sex;

var height;

run;

data class3;

    set class2;

    mean=mean(of col:);

    harmean=harmean(of col:);

    geomean=geomean(of col:);

run;

Reeza
Super User

Actually, Proc SQL is much clearer...

proc sql;

    create table summary as

    select sex, mean(height) as avg_height, count(height)/sum(1/height) as harm_mean, exp(mean(log(height))) as geom_mean

    from sashelp.class

    group by sex;

quit;

PQK
Calcite | Level 5 PQK
Calcite | Level 5

Reeza,

Many thanks for the replies.

After my first attempt with PROC MEANS failed, I also tried to create a table within PROC SQL using ‘group by’ to subset accordingly, but it never worked. After looking at your expression for geometric mean [that is, exp(mean(log(height)))], I realize the expression I used was incorrect. For some reason the mathematic formula I created in excel doesn’t work in SAS. I think it has something to do with array formulas.

In any case, both your SQL and data step method work to produce the correct geometric and harmonic mean! I am not that familiar with the table ODS destination, so I think I will stick with the SQL statement method instead.

Thanks again for your help!Smiley Happy

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

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.

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
  • 4 replies
  • 6624 views
  • 4 likes
  • 3 in conversation