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" <>
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-----
[]On Behalf Of Nick Cox
Sent: Wednesday, November 16, 2005 1:54 AM
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

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


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.


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:

*   For searches and help try:

© Copyright 1996–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index