Notice: On March 31, it was **announced** that Statalist is moving from an email list to a **forum**. The old list will shut down at the end of May, and its replacement, **statalist.org** is already up and running.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

From |
Nick Cox <njcoxstata@gmail.com> |

To |
"statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu> |

Subject |
Re: st: How to normalize variables in stata |

Date |
Thu, 25 Apr 2013 19:07:34 +0100 |

I'd call this standardisation, not normalisation. Your problem is essentially that of Paul Byatta posted yesterday http://www.stata.com/statalist/archive/2013-04/msg01088.html It seems that you have three variables -x1-, -x2-,-x3- and various -Period-s. forval i = 1/3 { egen mean = mean(x`i'), by(Period) egen sd = sd(x`i'), by(Period) gen N_x`i' = (x`i' - mean) / sd drop mean sd } should help. The problem with your code is the lines qui sum `x' if Period==`i', meanonly replace sd_`x'=r(sd) if Period==`i' With the -meanonly- option -summarize- does not calculate the sd. When you refer to r(sd) afterwards, Stata just substitutes missing, and this is propagated to all your new variables. But as above, you don't need so much code. Nick njcoxstata@gmail.com On 25 April 2013 18:53, Xixi Lin <winnielxx@gmail.com> wrote: > I am trying to normalize variables, and the following is my code: > foreach x in x1 x2 x3{ > gen N_`x' = . > gen mean_`x'=. > gen sd_`x'=. > } > forvalues i=2/120{ > foreach x in x1 x2 x3{ > qui sum `x' if Period==`i', meanonly > replace mean_`x'=r(mean) if Period==`i' > qui sum `x' if Period==`i', meanonly > replace sd_`x'=r(sd) if Period==`i' > } > } > > > forvalues i=2/120{ > foreach x in x1 x2 x3{ > replace N_`x'=(`x'-mean_`x')/sd_`x' if Period==`i' > } > } > > however, the above code does not working, because it creates all > missing values for standard deviation. Can anyone help me to fix the > code? * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/faqs/resources/statalist-faq/ * http://www.ats.ucla.edu/stat/stata/

**Follow-Ups**:**Re: st: How to normalize variables in stata***From:*Xixi Lin <winnielxx@gmail.com>

**References**:**st: How to normalize variables in stata***From:*Xixi Lin <winnielxx@gmail.com>

- Prev by Date:
**st: How to normalize variables in stata** - Next by Date:
**Re: st: question about the interaction term** - Previous by thread:
**st: How to normalize variables in stata** - Next by thread:
**Re: st: How to normalize variables in stata** - Index(es):