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

Hello, 

     I need help getting a geometric mean for all values on every date. For instance:

 

Date:                     Patients           Value

01/02/2023.           1                       3

01/02/2023.            2                     4

01/05/2023             1                      6

01/05/2023             2                       7

01/05/2023             3                      8 

 

What I want:

Date:                   Geometric Mean:

01/02/2023        3.46

01/05/2023        6.95

 

Thank you for any help you are able to provide! 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hello @pauliet987,

 

You can use PROC UNIVARIATE:

data have;
input Date :mmddyy. Patients Value;
format Date mmddyy10.;
cards;
01/02/2023 1 3
01/02/2023 2 4
01/05/2023 1 6
01/05/2023 2 7
01/05/2023 3 8
;

proc univariate data=have noprint;
by date;
var value;
output out=want geomean=geom_mean;
run;

proc print data=want;
format geom_mean 8.2;
run;

The BY statement assumes that the input dataset is sorted (or indexed) by date.

View solution in original post

2 REPLIES 2
FreelanceReinh
Jade | Level 19

Hello @pauliet987,

 

You can use PROC UNIVARIATE:

data have;
input Date :mmddyy. Patients Value;
format Date mmddyy10.;
cards;
01/02/2023 1 3
01/02/2023 2 4
01/05/2023 1 6
01/05/2023 2 7
01/05/2023 3 8
;

proc univariate data=have noprint;
by date;
var value;
output out=want geomean=geom_mean;
run;

proc print data=want;
format geom_mean 8.2;
run;

The BY statement assumes that the input dataset is sorted (or indexed) by date.

tarheel13
Rhodochrosite | Level 12

The other way is with proc means but you have to do take the log of value first like this:

data have2;
   set have;
   if value gt 0 then log_value=log(value);
run;

proc means data=have2 noprint;
   class date;
   var  log_value;
   ways 1;
   output out=stats mean= log_mean;
run;

data stats1;
   set stats;
   geomean=exp(log_mean);
run;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!

Register now

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 1017 views
  • 6 likes
  • 3 in conversation