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

RE: st: upper case to lower case for all variable names


From   "David Moore" <[email protected]>
To   [email protected]
Subject   RE: st: upper case to lower case for all variable names
Date   Wed, 23 Nov 2005 12:31:15 -0800

Another issue surfaces when any of the variable names is already lower case.  Then the rename command throws up because it's being
asked to rename a var to one that already exists.

A general solution, then, should not assume the left-hand side differs from the right-hand side.  A simple work around would prefix
the rename command with -capture-:

   foreach v of varlist _all {
      capture rename `v' `=lower("`v'")'
   }

This cautionary tale comes from first-hand experience, so it isn't necessarily unlikely

-----Original Message-----
From: [email protected]
[mailto:[email protected]]On Behalf Of Nick Cox
Sent: Wednesday, November 16, 2005 1:54 AM
To: [email protected]
Subject: RE: st: upper case to lower case for all variable names


It's worth explaining the bridge between
this and much shorter solutions given by
others.

The assumption that variable names go
from VAR1-VAR999 can be replaced by
a specification

<whateverisfirstinyourdata>-<whateverislastinyourdata>

and in turn by the wildcard * or _all.

Then you have no need to drop existing macros.
You can do the renaming on the fly:

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

A small risk with this last trick is that
"all variables" could include temporary variables
with names like __00000A. -rename- that to
__00000a and you will have programming problems.

However, when this is done it is, I guess,
virtually always with the main dataset, and interactively,
so no temporary variables are in sight.

Nick
[email protected]

Rafal Raciborski

> assuming your variables go from VAR1 to VAR999:
>
> macro drop _all
> foreach i of varlist VAR1-VAR999 {
> 	local j = lower("`i'")
> 	rename `i' `j'
> 	macro drop j
> }

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

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