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: rename
From
"Airey, David C" <[email protected]>
To
"[email protected]" <[email protected]>
Subject
RE: st: rename
Date
Thu, 9 Aug 2012 10:26:02 -0500
.
Thanks to Nick and David, I got my rename groups code to work. Using
macro expansion I create a local macro containing the new variable
names that are in a string variable in a data set that is correctly
ordered by row to match a wide dataset in terms of variable order. I
create another local of the variables names in the wide dataset to be
renamed using the unab command. Then the new rename groups command
works great here, for 2129 variables, where the oldvars and newvars
are represented by locals.
use markers_used.dta, clear
// make local containing new variable names
// from a string variable contents (same order
// as variables in dataset to be renamed)
gen rutgers2 = "m" + string(marker) if used == 0
replace rutgers2 = rutgers if used == 1
local end = _N
forvalues i = 1/`end' {
local next = rutgers2[`i']
local mynewvars `mynewvars' `next'
}
local mycount : word count `mynewvars'
display "`mycount'" // check if same as below
// get local containing old variable names
use chr2_geno.dta, clear
unab myoldvars : m*
local mycount : word count `myoldvars'
display "`mycount'" // check if same as above
// rename using new rename groups command
rename (`myoldvars') (`mynewvars')
drop m*
describe, short
// same effect in loop
/*
use chr2_geno.dta, clear
drop id famid
local nvars = r(k)
use chr2_geno.dta, clear
forvalues i = 1/`nvars' {
local oldname : word `i' of `myoldvars'
local newname : word `i' of `mynewvars'
rename `oldname' `newname'
}
drop m*
describe, short
*/
> sysuse auto
> rename (*) (<new list>)
>
> will work.
>
> Nick
>> Maybe you could run it through a loop where the first word of the first
>> macro gets changed to the first word of the second macro, etc.? Like
> this:
>>
>> * Make fake dataset
>> set more off
>> clear
>> set obs 1
>> forvalues i = 1/1000 {
>> gen oldvar`i' = .
>> }
>>
>> * Make list of new variable names
>> forvalues i = 1/1000 {
>> local newnames `newnames' newvar`i'
>> }
>>
>> * Change all variable names
>> local j = 1
>> unab original : oldvar*
>> forvalues j = 1/1000 {
>> local oldname : word `j' of `original'
>> local newname : word `j' of `newnames'
>> rename `oldname' `newname'
>> }
*
* 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/