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 at the end of May, and its replacement, statalist.org is already up and running.


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

RE: st: rename


From   "David Radwin" <dradwin@mprinc.com>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: rename
Date   Tue, 7 Aug 2012 14:04:40 -0700 (PDT)

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'
	local ++j
	}

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: owner-statalist@hsphsun2.harvard.edu [mailto:owner-
> statalist@hsphsun2.harvard.edu] On Behalf Of Airey, David C
> Sent: Tuesday, August 07, 2012 1:30 PM
> To: statalist@hsphsun2.harvard.edu
> Subject: Re: st: rename
> 
> .
> 
> If I had 1000 variables and wanted to rename them to a
> list of another 1000 names, what is the best way to do this?
> There are no stubs, so I cannot use the rename group command
> 
> rename (stub1-stub1000) (mystub1-mystub1000)
> 
> I can probably get the variable names inside a single local
> more easily after which I might expect this to work:
> 
> rename (`original') (`newnames')
> 
> > -rename- as such places absolutely no restrictions on the use of
> > locals in -rename- commands for the very simple and very fundamental
> > reason that all macro references are interpreted before -rename- gets
> > to work. -rename- never sees any macro references, just their
> > interpretations as macro contents.
> >
> > That said, let's emphasise that -rename-'s language is based on
> > extensive use of wildcards.
> >
> > Either way, the heart of David's question seems to be whether a common
> > stub is needed for -rename- to work well, if at all, and the answer is
> > No.
> >
> > -rename- can be used with a first argument as general as (*), meaning
> > all variables, so there are no presumptions on the structure of
> > variable names.
> >
> > Otherwise I think we need to see concrete examples to comment on
> > apparent difficulties here.
> >
> > Nick
> >
> > On Tue, Aug 7, 2012 at 6:27 PM, Airey, David C
> > <david.airey@vanderbilt.edu> wrote:
> >
> > > With the new rename group command, can you use locals to rename a
very
> > > large number of variables from an old name set to a new name set
where
> > > there will be no stub used in either set?
> > >
> > > The syntax is
> > >
> > > rename (old1 old2) (new1 new2)
> > >
> > > but can locals be used inside the parentheses to represent a long
list
> > > of variable names to be switched? I can generate the locals using
code
> > > rather than typing.
> > >
> > > Otherwise, what is the best way to rename a whole bunch of variables
> > > to new names where the variables do not have stubs.
> > >
> > > I can do the renaming using a reshape and merge, but it seems
clunky.
> 

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