Statalist The Stata Listserver

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

st: RE: accessing variables created within a foreach loop

From   "Alex Ogan" <>
To   <>
Subject   st: RE: accessing variables created within a foreach loop
Date   Fri, 9 Jun 2006 09:38:57 -0400

I'm not going to try to figure out the exact syntax with respect to
single/double/compound quotes.  However, you will be able to do
something similar to this:

Before starting the loop, create a local macro:

local MyVarlist ""

Within the loop, you can add to it:

local MyVarList = "`MyVarList'" + " " +  "`z'`i'"

Again, the quoting/spacing you need might be slightly different -- but
it looks like you needed to know how to append to a local macro varlist,
which this shows.

Hope this helps,

-----Original Message-----
[] On Behalf Of Vera E.
Sent: Friday, June 09, 2006 8:08 AM
Subject: st: accessing variables created within a foreach loop

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

This message is intended solely for the designated recipient(s). It may contain confidential or proprietary information and may be subject to confidentiality protections. If you are not a designated recipient, you may not review, copy, or distribute this message. If you receive this in error, please notify the sender by reply e-mail and delete this message. 

Arrowstreet Capital, L.P. is an Equal Opportunity Employer. 

*   For searches and help try:

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