Statalist The Stata Listserver


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

st: RE: Looping through a variable list


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Looping through a variable list
Date   Sat, 25 Feb 2006 17:44:33 -0000

Assuming that you are using Stata 9.1 (Statalist convention)
or even Stata >= 7 this can be done much more simply. 

foreach v in A B C D { 
	local x "`x' `v'" 
	regress y `x' 
} 

With strings always copy if you can, and
evaluate only if you must. 

Thus 

local x 

is better style than 

local x = "" 

In the example above I assumed no prior use 
of x. 

Nick 
[email protected] 

[email protected]
 
> I would like to loop my estimations through an increasing number of
> variables, but I am having difficulty undating my varlist.
> 
> For example, suppose there were four variable (A B C D) and I wish to
> estimate x on y where x is one of the four sets: "A", "A B", 
> "A B C", and
> "A B C D".
> 
> tokenize "A B C D"
> local i = 1
> local x = ""
> while i<=4 {
> local x = `x'+``i''
> regress y `x'
> local i=`i'+1
> }
> 
> This program does not work because I can not get x to update. 

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