Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: accessing variables created within a foreach loop


From   "Vera E. Troeger" <[email protected]>
To   [email protected]
Subject   st: accessing variables created within a foreach loop
Date   Fri, 9 Jun 2006 13:07:43 +0100 (BST)

I have the following problem, for a larger program I try to use the unit
means of some regressors (from a syntax varlist) in a later estimation.
I manage to get the unit means for all specified variables in varlist
(zvar in this case)
the part of the program would look as follows:



program define test
local i = "_m"
local zvar z1 z2 z4
    foreach z of local zvar {
        sort `ivar'
        tempvar `z'`i'
        by `ivar': egen ``z'`i''= mean(`z')
    }
end
test


what I do not manage is to "put" the newly generated ``z'`i'' variables
(from the loop) into a local macro (a varlist that I can use later on)
since I cannot refer to the `z'`i' once the loop is terminated. within
the loop I have no clue how to append each new generated variable
(`z'`i' ) to a local macro varlist. I know in this specific program the
newly generated variables are stored as local macros with the names z1_m,
z2_m and z4_m. but for this to be part of an estimator the variables in
zvar are specified by the user and I then cannot directly refer to them.
I hope I made myself sufficiently clear.Any advise would be
highly appreciated.
many thanks,
Vera

_______________________________________
Vera E. Troeger
Lecturer in Political Science
University of Exeter
Department of Politics
Amory Building
Rennes Drive
Exeter, Devon EX4 4QJ
UK
phone: +44.1392.264632

e-mail: [email protected]

Home:
39 Altamira
Topsham, Exeter EX3 0AQ
UK




*
*   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