Link to home
Start Free TrialLog in
Avatar of stochastic
stochasticFlag for India

asked on

Geometric Mean and Harmonic Mean in SAS

How does one compute Geometric Mean of a column in a SAS dataset? Also Harmonic mean?
It seems possible to compute GM of a row in SAS, but that would require using PROC TRANSPOSE. I couldn't find how to compute this directly for one or more columns. It seems  surprising that this fairly mundane descriptive statistic.

thanks in advance.

- stochastic
Avatar of andymcooper
andymcooper
Flag of United Kingdom of Great Britain and Northern Ireland image

Stochastic,

You're right - proc means will not do this. Why not???
Anyway, I think will the following SAS code will.
Note - comment out all lines using PRODUCT if you have a lot of variables >1, otherwise the numbers get too big!

/* dataset to try code on */
data tst;
  length VAR 8.;
  do VAR=1 to 10;
    output;
  end;
run;

/* datastep to calculate all sorts of means */
data tst_m (keep=ARITHMETIC_MEAN GEOMETRIC_MEAN_A GEOMETRIC_MEAN_B HARMONIC_MEAN);
  set tst end=EOF;
  retain COUNT PRODUCT SUM SUM_LOGS SUM_INV;
 
  length COUNT PRODUCT SUM SUM_LOGS SUM_INV ARITHMETIC_MEAN GEOMETRIC_MEAN_A GEOMETRIC_MEAN_B HARMONIC_MEAN 8.;
 
  LOG_VAR = log(VAR);

  /* initialise to 0 */
  if _N_=1 then do;
    COUNT= 0 ;
    PRODUCT=1;
    SUM_LOGS = 0;
    SUM =0 ;
    SUM_INV = 0;
  end;

  /* note - product gets too big in most cases, so sum logs instead */
  COUNT=COUNT+1;
  PRODUCT=PRODUCT*VAR;
  SUM_LOGS=SUM_LOGS+LOG_VAR;
  SUM=SUM+VAR;
  SUM_INV=SUM_INV+1/VAR;

  if EOF then do;
    ARITHMETIC_MEAN = SUM/COUNT;
    GEOMETRIC_MEAN_A = PRODUCT**(1/COUNT);
    GEOMETRIC_MEAN_B = exp((1/COUNT)*SUM_LOGS);
    HARMONIC_MEAN = 1/(SUM_INV/COUNT);
    output;
  end;
run;
ASKER CERTIFIED SOLUTION
Avatar of andymcooper
andymcooper
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of stochastic

ASKER

andymcooper,

I apologize for the delayed response. Thanks for your suggestions, I will try them out. Meanwhile
I'm accepting your comment as a solution. What remains is the curiosity about why this isn't a built-in feature.

- stochastic