Variable names and transformers


What are acceptable variable names? How do you use the commands -rename-, -renpfix-, and -renvars- to rename variable names?

A Stata variable name can contain up to 32 characters in any version of Stata (Small, Stata/IC, Stata/SE, or Stata/MP). Not all characters in the keyboard, however, are allowed in naming variables. A variable name may contain only the digits 0 to 9 and upper or lower case English alphabets (A to Z); and the first character cannot be a number. For example,

population
_Income
_1997
income0001
INC

are allowed, but not the following:

pop#
2gdppc
2007
~1997
POP/2001

Renaming variables are made easy by the commands -rename-, -renpfix-, and -renvars-. -renvars- (Weesie and Cox, 2005) is not available in the original package of Stata, but you may download and install it by typing:

net install dm88_1.pkg (see note below)


Do you need all these 3 renaming commands? Not really, but you will see that in some cases one is more convenient to use than the others.

If you only need to rename one variable, use -rename-. The syntax for -rename- is:

rename old_variable_name new_variable_name

For example, to rename the variable _Income to income, type:

rename _Income income

If, instead, you just want to replace the prefix of variables, use -renpfix-. The syntax for -renpfix- is:

renpfix old_prefix [new_prefix]

[Note: If new_prefix is not declared, -renpfix- returns the new variable names without the prefix provided they are allowed as variable names.]

For example, to change the prefix exportshare of the variables

exportshare1970
exportshare1980
exportshare1990
exportshare2000

to s_, type:

renpfix exportshare s_

This transforms the variables above to:

s_1970
s_1980
s_1990
s_2000

To drop the prefix export in the variable names, type:

renpfix export

This transforms the variables to:

share1970
share1980
share1990
share2000

But, you cannot do this:

renpfix exportshare

Why? Because, the first character of variable names cannot be numbers.

-renvars-, if installed, is the most flexible. It allows you to rename more than one variable at once, it can change the case of the variable names, and more. The syntax for -renvars- are:

renvars varlist \ new_varlist
renvars varlist, options

Examples:

renvars population income \ pop inc
/* renames population to pop and income to inc */

renvars population income, upper
/* renames population to POPULATION and income to INCOME */

renvars population income, trim(3)
/*renames population to pop and income to inc */

renvars population income, prefix(x_)
/renames population to x_population and income to x_income /

For more -renvars- options, type:

help renvars

Note: Sometimes “net install dm88_1” returns the error:

file http://www.stata.com/dm88_1.pkg not found
server says file temporarily redirected to http://www.stata.com/error/404.html
could not load dm88_1.pkg from http://www.stata.com/
r(601);

You may also install -renvars- by typing “search renvars”, then click on “dm88_1” and then click “(click here to install)”.

3 Responses

  1. How to remove certain suffixes of a variable of string, say
    name1
    name2
    name3i
    name4
    for some cases there is a unnecessary suffix “i” after the name# vaule, can I remove them, thank you in advance for your suggestion

  2. […] Stata, un procédé similaire est utilisé par la commande non-native renvars qui permet de renommer des groupes de variables en une seule ligne. Outre les noms de variables, […]

  3. this is nice! i used to do this with forvalues or foreach :)

Leave a Reply