Not applicable
Posts: 0

# Harmonic Mean

For the first time in my career, I have a need to calculate a harmonic mean across a large number of observations (harmean function is out and impractical).

I was completely surprised that proc means does not provide an option for harmonic and geometric means.

Is there a proc that has these alternate means available?
Not applicable
Posts: 0

## Re: Harmonic Mean

For Harmonic anyways why not:

Just augment your table with reciprocals then take the reciprocal of the reported mean from PROC MEANS?

Ike Eisenhauer
Frequent Contributor
Posts: 139

## Re: Harmonic Mean

Chuck

I assume you have many variables and many observations to process. So transposing the data and using the SAS function HARMEAN would be impractical. Imagine transposing a data set with millions of rows!.

This isn't the most elegant solution but it gets the job done. With a little work I am confident that you can come up with an elegant solution for you specific problem.

I tried to mimic the harmean function in SAS. In the SAS documentation if any value is negative then the result is missing. And missing values need to be ignored.

data one;
input x;
datalines;
.
4
12
24

;
run;

data two;
set one end=eof;

if x <0 and not missing(X) then negative_flag+1;
x1=1/x;
cum_x+x1;
n+1;
if missing(x) then n=n-1;

if eof and not negative_flag then do;
x_harmonic_mean=1/(cum_x/n);
put x_harmonic_mean;
end;
run;

data _null_;
x2=harmean(.,4,12,24);
put x2=;
run;

-Darryl
Discussion stats
• 2 replies
• 696 views
• 0 likes
• 2 in conversation