Statalist


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

RE: st: Changing the suffix on variable names


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: Changing the suffix on variable names
Date   Fri, 1 May 2009 13:25:28 +0100

Assume that you didn't know about -renvars- from Stata Journal, as just publicised by Martin Weiss. You could still slim this down and speed this up: 

1. There is no need to push the wildcard varlist *_56epi through -ds- and then push its result r(varlist) into a local. This is repackaging the same information in three forms; one is enough.  

2. There is no need for the family of middle macros y_`x' inside the loop. 

Thus this example can be slimmed to: 

clear
set obs 1000
g abc_56epi=0
g def_56epi=0
g ghi_56epi=0
foreach x of var *_56epi {
	rename `x' `= regexr("`x'", "_56epi", "_45epi")' 
}

Nick 
[email protected] 

Tirthankar Chakravarty

Here is another, clunkier, option:

/* Begin */
clear
set obs 1000
g abc_56epi=0
g def_56epi=0
g ghi_56epi=0
ds *_56epi
local varlist=r(varlist)
foreach x of local varlist {
	local y_`x' = regexr("`x'", "_56epi", "_45epi")
	rename `x' `y_`x''
}
/* End */

T


On Fri, May 1, 2009 at 11:57 AM, sdm1 <[email protected]> wrote:
> I have 150 variables all with the same suffix.  I'd like to be able to
> change this suffix to a different suffix (eg from '_56epi' to '_45epi'.  Is
> there an easier way to do this than using the 'rename' command 150 times?

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