Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: A loop for each couple of variables (instead of for each variable)
From
Maarten Buis <[email protected]>
To
[email protected]
Subject
Re: st: A loop for each couple of variables (instead of for each variable)
Date
Mon, 12 Sep 2011 13:09:48 +0200
On Mon, Sep 12, 2011 at 12:53 PM, Fabien Bertho wrote:
> I created a loop in order to estimate various specifications of my model
<snip>
> Now, I would like to run this loop not for two variables but for two locals that comprises two variables. I tried the following code.
<snip>
> But it does not work because during the loop Stata splits my local into two variables…
You can nest a macro into a macro name, so if your loop creates a
macro `i' that contains 1 in the first iteration, 2 in the second
iteration, etc. than you can refer to a local `var`i'', which will be
read as `var1' in the first iteration, `var2' in the second iteration,
etc.
*-------------- begin example -------------
sysuse auto, clear
local var1 "length weight"
local var2 "gear_ratio turn"
forvalues i = 1/2 {
reg mpg `var`i''
}
*------------ end example -----------------
(For more on examples I sent to the Statalist see:
http://www.maartenbuis.nl/example_faq )
Another solution is to store these varlists in one local and surround
each sub-varlist with quotes. The entire string than needs to be
surrounded with compound quotes, see -help quotes-.
*-------------- begin example -------------
sysuse auto, clear
#delim ;
local vars
`"
"length weight"
"gear_ratio turn"
"' ;
#delim cr
foreach var of local vars {
reg mpg `var'
}
*------------ end example -----------------
(For more on examples I sent to the Statalist see:
http://www.maartenbuis.nl/example_faq )
Hope this helps,
Maarten
--------------------------
Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen
Germany
http://www.maartenbuis.nl
--------------------------
*
* 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/