Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

revising -rename- [was:RE: st: quick question]


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   revising -rename- [was:RE: st: quick question]
Date   Thu, 26 Feb 2004 12:48:20 -0000

To back up Gary: the solutions are indeed complementary. 

As several people pointed out, the key is to convert the 
name of the variable to lower case, not its values (which 
would not even make sense if the variable was numeric). 

-renvars- is a canned solution: how to use it should be clear 
from the help. If not, the authors did a lousy job. 

A -foreach- loop can be written on the fly, but for this 
problem and for many others requires some fluency with 
Stata functions, or recourse to the help. Also, Gary's 
code can be stream-lined, possibly at the cost of some clarity: 

foreach v of var * { 
	capture rename `v' `=lower("`v'")'
} 

That's my favourite solution to the problem posed -- 
so long as you know all the syntax used -- and unless 
you're a Stata programmer, you could very easily be 
unfamiliar with one or more of -foreach-, local macros, 
-capture- and expansion operators. 

A problem with -renvars- is that it doesn't collapse 
to -rename- as a special case. That is, it doesn't 
allow the syntax 

. renvars oldname newname 

Indeed, a bunch of Stata ultra-heavyweights (1 President + 
2 Vice-Presidents) once rightly disparaged -renvars- for its 
"arbitrary syntax", although they didn't say what its syntax
_should_ be. 

So let's try that. Start with -rename-, and redesign it to 
cope with more general cases. Adding a bunch of options,
like those of -renvars- or something similar, is not a big
deal. It's designing what comes before the comma which is 
the key detail. 

The implementation I'll optimistically assign
to StataCorp, as this really should _not_ be an .ado. It
should be part of the executable. The existing syntax 
(which I'll call Syntax 1) is 

Syntax 1: . rename oldname newname

(and for compatibility we really must keep that) and 
it could generalise to 

Syntax 2: . rename oldname1 [oldname2 [oldname3 ... ]] 
	newname1 [newname2 [newname3 ... ]] 

or  

Syntax 2:. rename oldvarlist newvarlist 

or to  

Syntax 2': . rename oldname1 newname1 [oldname2 newname2 
	[ oldname3 newname3 ... ]] 

Both of those collapse to the existing syntax. 

So why didn't -renvars- do that? I've forgotten
the history, and probably Jeroen Weesie has too, 
but in practice with lots of renaming it's easier 
than it should be to lose track of where you are. 
That's why we introduced \ as a separator, so 
that you can go "old names this way", "new names
that way"

. renvars oldvarlist \ newvarlist 

or even 

. renvars \ newvarlist 

But that "\" still sticks in the craw, stylistically, 
bringing back distant memories of -for-. And "/", 
which is allowed but undocumented, is no better for 
this purpose. 

Nevertheless, it seems to me that Syntax 2 
(which is more compatible with wildcards and variable
name ranges) should be allowed in -rename-. 

Also I suggest that 

Syntax 3: . rename oldvarlist, new(varlist) 

with a special case 

. rename , new(varlist) 

should be allowed. The latter often is very 
convenient. You have inherited some mess (e.g. 
from a spreadsheet); you can see the names in 
front of you (say in the Variables Window or 
through -describe- or -ds-); you just want to 
clean up.  

So here's my suggestion for a generalised -rename-, 
for discussion. 

Syntax 1: . rename oldname newname 

Syntax 2 (syntax 1, generalised): . rename oldvarlist newvarlist 

Syntax 3: . rename [oldvarlist] [, new(varlist) <renvars_options>]  

The syntax is, naturally, more complicated, but users could 
easily choose. Quite a few Stata commands are now written 
up in terms of some basic syntax (beginners start here) and 
more advanced syntax. 

Nick 
[email protected] 

Gary Longton replied to Peter Lachenbruch

> > I have a data set that I got with some oddball names like PyrexIA or
> > CuTaneousDA, etc. and I'd like to rename everything so all 
> characters were
> > lowercase.  Is there an easy way to do this via a foreach 
> loop and a simple
> > command, or do I have to 
> > rename PyrexIA pyrexia
> > rename CuTaneousDA cutaneousda
> > 
> > I looked at the lower function but that seems to operate on 
> variables rather
> > than names.
> 
> Richard Williams, Roger Newson, and Richard Goldstein 
> suggested -renvars-, a 
> single command solution which will do this in one fell swoop.
> But I think you were also on the right track with what Nick 
> Cox might call a 
> "first principles" approach, using -foreach- and the lower() 
> function.  You just 
> need to ensure that lower() is looking at the string variable 
> name as an 
> argument, rather than at the variable itself, by enclosing in 
> double quotes.  Eg.
> 
>     foreach var of varlist _all {
>         local newname = lower("`var'")
>         if "`var'" ~= "`newname'" {
>             rename `var' `newname'
>         }
>     }
> 
> should do it.

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   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