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   "David Radwin" <[email protected]>
To   <[email protected]>
Subject   RE: st: rename
Date   Thu, 9 Aug 2012 09:44:50 -0700 (PDT)

Also, if you want to automate the process for checking whether the number
of variables in the old list and the new list are the same, you could give
the two mycount macros different names and use the -assert- command like
this:

. assert `oldcount' == `newcount'

See Gould, W. 2003. Stata tip 3: How to be assertive. Stata Journal 3:
448. http://www.stata-journal.com/article.html?article=dm0003

David

--
David Radwin
Senior Research Associate
MPR Associates, Inc.
2150 Shattuck Ave., Suite 800
Berkeley, CA 94704
Phone: 510-849-4942
Fax: 510-849-0794

www.mprinc.com


> -----Original Message-----
> From: [email protected] [mailto:owner-
> [email protected]] On Behalf Of Nick Cox
> Sent: Thursday, August 09, 2012 8:32 AM
> To: [email protected]
> Subject: Re: st: rename
> 
> 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/
*
*   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