Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: RE: foreach to renumber vars


From   Nick Cox <n.j.cox@durham.ac.uk>
To   "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
Subject   st: RE: foreach to renumber vars
Date   Tue, 7 Sep 2010 17:43:35 +0100

-forval- is better:

	forval i = 2/100 { 
		local j = `i' - 2 
		rename x`i' y`j'
	} 

Or 

	forval i = 2/100 { 
		rename x`i' y`=`i'-2' 
	}

A -foreach- solution could be 

	foreach i of numlist 2/100 { 
		<same innards> 
	} 

But it has no advantages over -forval-. 

See also -renvars- from the SJ. 

Nick 
n.j.cox@durham.ac.uk 

B. Timothy Walsh

I would like to rename/renumber variables of the format x2 - x100 to y0 - 
y98. In other words, I'd like to subtract 2 from the "subscript".
I'm afraid I am still not skilled enough to figure out how to do this with 
foreach and a macro.

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


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index