Dear Nick,
thanks a lot for your kind and helpful hints. Reading across your reply, I
notice that In my previous attempts to solve the problem by myself I have
mixed up the lines you wrote about -foreach- and -forval- (:
Thanks a lot again and Kind Regards,
Carlo
-----Messaggio originale-----
Da: [email protected]
[mailto:[email protected]] Per conto di Nick Cox
Inviato: martedì 21 ottobre 2008 19.32
A: [email protected]
Oggetto: st: RE: re: a question related to -foreach-
Notwithstanding this good advice, it may be of interest to see an answer
to the original question.
First, as does Kit, I assume that the number of observations is at least
36. This can always be achieved by -set obs- if it is not correct.
gen mean = .
local i = 1
qui foreach x of var b1-b36 {
su `x', meanonly
replace mean = r(mean) in `i'
local ++i
}
OR
gen mean = .
qui forval i = 1/36 {
su b`i', meanonly
replace mean = r(mean) in `i'
}
You will want to keep track of names too.
For example,
gen mean = .
gen varname = ""
local i = 1
qui foreach x of var b1-b36 {
su `x', meanonly
replace mean = r(mean) in `i'
replace varname = "`x'" in `i'
local ++i
}
Nick
[email protected]
Christopher Baum
Carlo asks
. foreach x of varlist b1-b36 {
2. summarize `x'
3. }
I would like to generate a newvar in which the r(mean) for each one
of the 36 variables included in varlist are stored. How can I do this
in Stata 9.2/SE?
No need to use a foreach loop.
tabstat b1-b36, save
mat mu = r(StatTotal)'
mat li mu
If for some reason you want these in a variable, use -svmat-:
svmat mu
which creates variable mu1, obs. 1-36.
*
* 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/