Statalist The Stata Listserver

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

st: RE: RE: accessing variables created within a foreach loop

From   "Nick Cox" <>
To   <>
Subject   st: RE: RE: accessing variables created within a foreach loop
Date   Fri, 9 Jun 2006 18:10:50 +0100

This can be simplified and corrected (slightly) 
and debugged (again slightly). 

It is a somewhat theological question, but 
assigning an empty string to a local macro 
name does not really _create_ that macro, at least
so far as what the programmer can do with it. That is, 
suppose that the local MyVarlist does not 
exist. Then any reference to 


is treated as if it held an empty string. 
So, practically, the statement 

local MyVarlist "" 

makes no difference, as afterwards the
statement just above still holds. 

For loops like those discussed here 

local MyVarlist ""

or indeed 

local MyVarlist 

has one important advantage: if the same 
name has been used earlier in the same 
program, it clears the contents. This 
is usually essential in that case; 
otherwise, it does no harm, although
it is not essential. 

For mere accumulation of names the 

local MyVarlist `Myvarlist' `z'`i' 

is cleanest and best, although it 
can take a little getting used to. 

Some people prefer to distinguish 

local <macroname> "<new contents>" 

more clearly, as by 

local MyVarlist "`MyVarlist' `z'`i'" 

or by 

local MyVarlist "`MyVarlist'`z'`i' " 

(watch the spaces), although in 
each case a leading or trailing 
space can mess up subsequent output 
a little. The fact that the first 
time this loop is entered `MyVarlist' 
may be empty is not a problem. 

Finally, note here no use of = or + 
which turn the RHS into an expression
to be evaluated. With explicit operators, 
limits on the length of expressions that
can be evaluated bite much sooner, a possible
bug if expressions get rather long. 
This is less of a problem in recent versions of Stata
than it used to be. 


Alex Ogan
> 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.

Vera E. Troeger
> 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,

*   For searches and help try:

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