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]

st: RE: changing names from UPPER to lower case


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: changing names from UPPER to lower case
Date   Fri, 19 Mar 2010 18:24:16 -0000

Tony asked a good question that sparked replies from Kit Baum, Eric
Booth, Maarten Buis, Eric de Souza and Martin Weiss. 

Eric Booth's reply could be missed because he sent it in as a reply to a
different question. 

That still leaves a little room for a miniature tutorial linking the
replies together and adding a few more details. 

Let's simplify Tony's question to imagine that there are three variables
X Y Z, as the main issues remain the same. 

The overall logic is easily reconstructed: 

1. Tony doesn't like those names SHOUTING at him. 

2. He knows that -rename- changes variable names and that the syntax is 

rename <old_varname> <new_varname> 

3. So the idea is to loop over the variable names and apply -rename- one
by one. 

The logic is good. In fact, in Stata this is the only kind of solution
possible (other than repeated individual -rename-s) and other solutions
all turn out to be the same thing implemented otherwise. 

Tony's code is equivalent to 

foreach v of var X Y Z { 
	rename `v' lower("`v'") 
} 

-- but it fails. Making things concrete, first time around the loop you
have 

rename X lower("X") 

but -rename- expects <new_varname> where it gets -lower("X")- and it
complains. In fact it goes "lower" -- fine so far, that could be (part
of) a new variable name -- but the parenthesis "(" cannot be part of a
(legal) new variable name, so that's that. The command fails, and so the
loop fails. 

Note the difference here between -lower(X)- and -lower("X")-. The first
does what you want if and only if the name required is the value of the
first observation of a string variable X -- which is not impossible
given e.g. some spreadsheet-type mess, but not what is wanted here. "X"
signifies that you want the literal string "X". 

What is needed is evaluation of the expression -lower("X")- so that
-rename- sees the result of that evaluation, not the expression. The two
main ways to do that are via a local macro 

foreach v of var X Y Z { 
	local new = lower("`v'") 
	rename `v' `new' 
}

or on the fly

foreach v of var X Y Z { 
	rename `v' `=lower("`v'")' 
}

That choice is a matter of taste. People used to the latter tend to use
it, but it's not compulsory if it looks confusing. 

That said, there are convenience commands that do this for you. They all
boil down to wrappers for the logic above. One mentioned was -tolower-
from SSC. Typing 

. ssc type tolower.ado

lets you see that this beast is, in its entirety, 

* Nick Cox Statalist Digest 895
program def tolower 
        version 5.0 
        local varlist "opt ex" 
        parse "`*'" 
        parse "`varlist'", parse(" ") 
        while "`1'" != "" { 
                local newname = lower("`1'") 
                rename `1' `newname' 
                mac shift 
        }
end

Well, that's relatively short and simple and it's notable (to
StataCorp's credit, not the author's) that this works as well as it did
first in 1995 (it seems). But it is easy to break it. 

tolower X Y Z 

would do what Tony intends but -tolower- is not smart enough to cope
with a mix of upper and lower case names, which you want to make uniform
as lower case. 

. rename x x 

fails because what is offered as a new varname, the second variable x,
is not a _new_ variable name, and already exists. -tolower- with x
included would fail for the same reason. 

-tolower- is a poor program, but SSC is not a compendium of the best
programs available in 2010, but an archive which has a modicum of older
stuff, in this instance just in case someone has a script or do file or
program with -tolower- built in. 

A much versatile renaming command -renvars- was also mentioned (but NB
that it does not lurk on SSC, but in the Stata Journal files).

renvars, lower 

is one way of tackling Tony's problem. It is smart enough to cope with a
mix of cases and can do other things too, and I think it's objectively
true that it's a better program than -tolower-. 

The ugly name -renvars- (due to Jeroen Weesie, one of its authors) was
chosen to be different enough from -rename- (and perhaps, to paraphrase
the great philosopher C.S. Peirce in another connection, because it is
ugly enough to be safe from kidnappers). 

Nick 
n.j.cox@durham.ac.uk 

Lachenbruch, Peter

I have a number of variables in uppercase that I want to change to lower
case.  I can do it individually, of course, but when I try to do it in a
foreach loop  I get

. foreach v of varlist ILD-CUTIC_OG{
  2. rename `v' lower(`v')
  3. }
 ( not allowed
 r(101);

. foreach v of varlist ILD-CUTIC_OG{
  2. rename `v' lower("`v'")
  3. }
 ( not allowed
 r(101);

Apparently rename doesn't allow this function.
I went ahead with the individual changes, but is there a simple way that
I'm
missing?  I've looked at the manual entries for the lower function, the
rename command, etc.

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