sasgeometric-mean

Geometric mean in SAS for each column of a data set


Suppose I have the following SAS data set

data have; 
input id$ x1 x2 x3;
datalines;
1 10 5 15
2 15 10 7
3 12 10 9
4 14 15 10
;
run;

I would like to calculate the geometric mean, geometric coefficient of variation (formula is 100*(exp(ASD^2)-1)^0.5 ASD is the arithmetic SD of log-transformed data.) of each variable x1, x2, and x3. How can I perform these operations? As I am completely new in SAS, I would appreciate your support.


Solution

  • *Compute log value of x1, x2, x3;
    data tab1;
      set have;
    
      logx1=log(x1); 
      logx2=log(x2); 
      logx3=log(x3); 
    run;
    
    *Compute SD of logx1, logx2, logx3;
    proc means data=tab1 noprint;
      var logx1-logx3;
      output out=tab2 n=n1-n3 std=std1-std3;
    run;
    
    *Compute logcv using formula;
    data tab3;
      set tab2;
    
      logcv1=100*(exp(std1**2)-1)**0.5;
      logcv2=100*(exp(std2**2)-1)**0.5;
      logcv3=100*(exp(std3**2)-1)**0.5;
      putlog 'NOTE: ' logcv1= logcv2= logcv3=;
    run;
    

    The result is show in log window:

    NOTE: logcv1=18.155613536 logcv2=48.09165987 logcv3=32.538955751
    

    It not much diffcult to do caculation in SAS, just try to do it step by step and you will get it.