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 |

Subject |
Re: st: loop with egen |

Date |
Fri, 8 Feb 2013 14:00:47 +0000 |

If that's true, then foreach v of var <varlist> { qui su `v' gen mean_`v' = r(mean) gen sd_`v' = r(sd) } is a more efficient way of doing it. -egen- has lots of stuff that is irrelevant for your purpose. I've found that for occasional graphics tricks it is helpful to put variables into constants. Otherwise I think my "usually" is still true. On Fri, Feb 8, 2013 at 1:51 PM, Nahla Betelmal <nahlaib@gmail.com> wrote: > Dear all > > many thanks Rich, and yes I had the variables in my command already. > this was not the problem. The issue was with each variable new name. > > Thanks Nick, the loop works perfectly. I need to generate the > constants as variables to use in further equations and calculations > instead of writing them manually. > > Many thanks again > > Nahla > > > On 8 February 2013 13:38, Nick Cox <njcoxstata@gmail.com> wrote: >> I don't believe that was your code. >> >> I suspect that you wrote something of the form >> >> foreach v of var <varlist> { >> egen mean = mean(`v') >> egen sd = sd(`v') >> } >> >> where instead of <varlist> you had a list of variable names. >> >> Second time around the loop, the problem is that -mean- already >> exists, and so the loop fails. >> >> You could make this loop legal by ensuring that each new variable has >> a different name >> >> foreach v of var <varlist> { >> egen mean_`v' = mean(`v') >> egen sd_`v' = sd(`v') >> } >> >> but that would still be a bad idea. There is usually little point in >> generating lots of variables that just contain constants. >> >> su <varlist> will give you a list of means and standard deviations. >> >> For more precise control, consider >> >> . sysuse auto >> (1978 Automobile Data) >> >> . tabstat mpg weight, s(mean sd) c(s) format(%2.1f) >> >> variable | mean sd >> -------------+-------------------- >> mpg | 21.3 5.8 >> weight | 3019.5 777.2 >> ---------------------------------- >> >> Nick >> >> On Fri, Feb 8, 2013 at 1:29 PM, Nahla Betelmal <nahlaib@gmail.com> wrote: >> >>> I am trying to generate the mean and standard deviation for more than >>> 30 variables. I tried to do that using the following loop >>> >>> foreach v of var { >>> egen mean = mean(`v') >>> egen sd = sd(`v') >>> } >>> >>> however, it only generate the mean and standard deviation for the >>> first variable only. >>> >>> I got this message :mean already defined >>> r(110); >>> >>> what should I add to the loop , so it can generate the mean and >>> standard deviation for each variable in separate column? * * 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/

**References**:**st: loop with egen***From:*Nahla Betelmal <nahlaib@gmail.com>

**Re: st: loop with egen***From:*Nick Cox <njcoxstata@gmail.com>

**Re: st: loop with egen***From:*Nahla Betelmal <nahlaib@gmail.com>

- Prev by Date:
**Re: st: Bootstrapping question** - Next by Date:
**st: FW: forvalues question...** - Previous by thread:
**Re: st: loop with egen** - Next by thread:
**st: FW: forvalues question...** - Index(es):