Bookmark and Share

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   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: rename
Date   Thu, 9 Aug 2012 16:32:23 +0100

Good news. You may be interested for future coding in small
abbreviations of your code.

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'
}

becomes

/// assumes only values are 0 or 1
gen rutgers2 = cond(used == 0, "m" + string(marker), rutgers)

forvalues i = 1/`=_N' {
        local mynewvars `mynewvars' `=rutgers[`i']'
}

On Thu, Aug 9, 2012 at 4:26 PM, Airey, David C
<[email protected]> wrote:
> .
>
> 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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index