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" <>
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')

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 E. Troeger
Lecturer in Political Science
University of Exeter
Department of Politics
Amory Building
Rennes Drive
Exeter, Devon EX4 4QJ
phone: +44.1392.264632


39 Altamira
Topsham, Exeter EX3 0AQ

*   For searches and help try:

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