Statalist The Stata Listserver


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

Re: st: Looping through a range of variables


From   Philipp Rehm <Philipp.Rehm@gmx.de>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Looping through a range of variables
Date   Fri, 17 Nov 2006 16:21:08 +0100

You can ask -foreach- to accept a varlist.

Example:
sysuse auto, clear
foreach var of varlist price-turn {
rename `var' _`var'
}

Note that this relies on the order of the variables in your data-set. The following example shows this:

sysuse auto, clear
aorder
foreach var of varlist price-turn {
rename `var' _`var'
}

HTH,
Philipp



Rachel wrote:

I'd like to loop through a large number of variables that don't have
common prefixes or suffixes.   So I'd like to create a loop that goes
through all variables in a range (e.g. Var1-New7 where the variables
are Var1 Var2 New3 New7.).

"Foreach" requires a list, not a range, so that doesn't work.  And
ForValues obviously only accepts numbers.

Is there a way of doing this?

Thanks,
Rachel
*
*   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/

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index