 Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: How to standardize a variable with a define mean

 From Alberto R Osella To statalist@hsphsun2.harvard.edu Subject Re: st: How to standardize a variable with a define mean Date Wed, 01 Feb 2012 12:34:11 +0100

```Thanks NIck!!

Alberto R. Osella, MD, PhD
Laboratorio di Epidemiologia e Biostatistica
IRCCS Saverio de Bellis
Via Turi, 27
70013 Castellana Grotte (BA)
Italia
Tel:    +39 0804994655
Fax:    +39 0804994650
e-mail: arosella@irccsdebellis.it

Il 01/02/2012 12:23, Nick Cox ha scritto:
```
```Maarten is quite correct here in fixing your syntax, but note that -egen- is not needed here given that you have already -summarized- BMI.

su BMI
gen stdmi = (BMI - r(mean)) / r(sd)

That way the whole business of putting value of r(mean) into a scalar, only to have to take it out again, is avoided.

Even going the -egen- route the -scalar- is not needed and you don't even need to feed r(mean) to -egen- as by default the -std()- function works with the mean and standard deviation of the expression specified, here a variable name.

. egen stdmpg1 = std(mpg)

. su mpg

Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
mpg |        74     21.2973    5.785503         12         41

. gen stdmpg2 = (mpg - r(mean)) / r(sd)

. su stdmpg*

Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
stdmpg1 |        74   -7.00e-09           1  -1.606999    3.40553
stdmpg2 |        74   -7.00e-09           1  -1.606999    3.40553

So, the problem needs one line or two, depending on whether you choose convenience or Stata minimalism, but not more.

Nick
n.j.cox@durham.ac.uk

Maarten Buis

On Wed, Feb 1, 2012 at 10:44 AM, Alberto R Osella wrote:
```
```I want to standardize a variable (BMI) with a given mean value. I've
obtained that value from a -summarize- command, that is r(mean).
Now I want to pass the value of r(mean) to the egen command. Here the
commands and the error returned by Stata.

. sum BMI
. scalar meanbmi=r(mean)
. display meanbmi
25.8435
. egen stdbmi=std(BMI), mean(meanbmi) std(1)
option mean() incorrectly specified
r(198);
```
```try:

egen stdbmi=std(BMI), mean(`=meanbmi') std(1)

The trick is that meanbmi is the name of a scalar while the -mean()-
option expects a number. By surrounding that scalar name with " `= "
and " ' " you are forcing Stata to first evaluate the scalar name,
resulting in a number, which is exactly what the -mean()- option
wanted.

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

```
```*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```