Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Re: return list and by


From   "Michael Blasnik" <[email protected]>
To   <[email protected]>
Subject   st: Re: return list and by
Date   Mon, 12 Apr 2004 17:59:20 -0400

most directly:

by name: egen overall2=mean(`i')

will put the mean of var `i', calculated for each value of name, in
overall2.  Of course, this value will still be overwritten by the next var
in the varlist if your loop is left as shown.

More specifically, when you use constructs such as -by x: summ y- , you
can't get at the individual results -- it's quite frustrating when you first
discover this.  Instead you either have to write your own loop across
groups, or else you may be able to use an -egen- function or -statsby- (or
perhaps -parmby-).

Michael Blasnik
[email protected]


----- Original Message ----- 
From: <[email protected]>
To: <[email protected]>
Sent: Monday, April 12, 2004 5:21 PM
Subject: st: return list and by


> Hello All --
> Is there a way to save the mean in r() for each value of a
> variable?  More specifically, I want the mean for each
> different person contained within the variable 'name'.  My
> programming experience in Stata is quite limited so I'd
> appreciate any advice; the shrapnel from my attempt at coding
> is pasted below:
>
> program mean_get
> syntax [varlist]
> foreach i of local varlist {
> generate overall2 = 0
> by name, sort:  summarize `i', meanonly
> local 1 = r(mean)
> by name, sort:  replace overall2 = `1'
> }
> end
>
> When I run this, it returns the mean from the last value in
> 'name' and uses it to replace all values of 'overall2'.  Any
> suggestions?
>
> Many thanks...
> Clint Thompson
>
>
> *
> *   For searches and help try:
> *   http://www.stata.com/support/faqs/res/findit.html
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/


*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index