Technology Tales

Adventures in consumer and enterprise technology

Calculating geometric means using SAS

Published on 19th March 2012 Estimated Reading Time: 2 minutes

Recently, I needed to calculate geometric means after a break of a number of years and ended racking a weary brain until it was brought back to me. In order that I am not in the same situation again, I am recording it here and sharing it is always good too.

The first step is to take the natural log (to base e or the approximated irrational value of the mathematical constant, 2.718281828) of the actual values in the data. Since you cannot have a log of zero, the solution is either to exclude those values or substitute a small value that will not affect the overall result, as is done in the data step below. In SAS, the log function uses the number e as its base, so you need to use the log10 equivalent when base 10 logs are needed.

data temp;
    set temp;
    if result=0 then _result=0.000001;
    else _result=result;
    ln_result=log(_result);
run;

Next, the mean of the log values is determined, and you can use any method of doing that so long as it gives the correct values. PROC MEANS is used here but PROC SUMMARY (identical to MEANS except it defaults to data set creation while that generates output by default; for that reason, we need to use the NOPRINT option here), PROC UNIVARIATE or even the MEAN function in PROC SQL.

proc means data=temp noprint;
    output out=mean mean=mean;
    var ln_result;
run;

With the mean of the log values obtained, we have to take the exponential of the obtained value(s) using the EXP function. This returns values of the same magnitude as in the original data, using the formula emean.

data gmean;
    set mean;
    gmean=exp(mean);
run;

Comment:

  • The content, images, and materials on this website are protected by copyright law and may not be reproduced, distributed, transmitted, displayed, or published in any form without the prior written permission of the copyright holder. All trademarks, logos, and brand names mentioned on this website are the property of their respective owners. Unauthorised use or duplication of these materials may violate copyright, trademark and other applicable laws, and could result in criminal or civil penalties.

  • All comments on this website are moderated and should contribute meaningfully to the discussion. We welcome diverse viewpoints expressed respectfully, but reserve the right to remove any comments containing hate speech, profanity, personal attacks, spam, promotional content or other inappropriate material without notice. Please note that comment moderation may take up to 24 hours, and that repeatedly violating these guidelines may result in being banned from future participation.

  • By submitting a comment, you grant us the right to publish and edit it as needed, whilst retaining your ownership of the content. Your email address will never be published or shared, though it is required for moderation purposes.