When the new dataset is imported or generated, you may discover that the variable names do not correspond to your task. Perhaps they do not correspond to the variables in another dataset you wish to merge. In such a situation, rename commands help in resolving this issue. The Rename command is used to rename variable in Stata This command is not only used to rename a single variable. It can also rename a group of variables, change the variables’ case styles, and add/remove prefixes or suffixes. Rename command will only change the variable’s name, and the variable’s content will remain the same.
This article will cover all the possible use of the rename command in Stata. To understand this, we will use an auto dataset. The data can be accessed using the below command in Stata.
This dataset contains the following variables.
Related Article: Order Variables in Stata
Rename Single Variable using rename Command in Stata
The below rename syntax can be used to rename the variable name in Stata. First, we will write the command
rename, the old name of the variable, and the new name of the variable. Remember to add the space between the rename, old variable, and new variable names.
rename old_varname new_varname
For example, we want to change the name of the variable “make” to “car_model”. We can also name it a carmodel, but the underscore can make it easier to understand.
rename make car_model
You will notice now the first variable, “make” name, has changed to “car_model”.
The help menu of rename command can be used to view the syntax. By using the below command, the help menu will be opened.
This rename can also be done through the variable manager. You will find the variable manager menu option in the toolbar.
The variable manager can also be accessed using the below command.
Let’s change the name of the “price” variable to “price_car”. You will select the variable “price” and add the new name “price_car” to the option available under variable properties.
Renaming Multiple Variable
To change the name of 10 or more variables at once, we use the rename command for group variables. You can find the rename command for group variables below. First, we will write the rename command followed by a space. Then in parentheses, we will write all the variables whose names we want to change; then, after space, we will write the new names of the variables.
rename (old1 old2 ...) (new1 new2 ...) [, options1]
For example, we want to rename the “price” as “price_in_dollars”, “mpg” as “Millage” and “rep78” as “Repair”. So instead of using three different commands, we will use one command given below.
rename (price mpg rep78) (Price_in_dollars Millage Repair)
Rename of group variable can also be done following the below path.
Data > Data utilities > Rename groups of variables
Select the variables those names you want to change from the drop menu of the existing variable name and write new variable names and click OK.
Related Article: Using Sort command in Stata
Change Case Style of Variables
The rename command also changes the variable’s upper, lower, and proper case. The upper option will make all the letters of the variable name capital. Lower will make all the letters of the variable small. Proper will make the first letter of the variable name capitalized, and the rest will be small. The below commands will help change the case into upper, lower, and proper. First, we will write rename, then after space the name of the variable, and lastly, after the comma, we choose the option (upper, lower, proper)
rename varname,upper rename varname,lower rename varname,proper
We can use an asterisk instead of variable names to change the case style of all the variables at once. Here, an asterisk is a wild card that indicates zero or more characters (all variables specifically in this case).
rename *,upper rename *,lower rename *,proper
The above picture shows that some variables are lowercase, and some have the first letter capitalized. So, we can rename commands to adjust them.
For example, we want all the first letters of the “length” variable capital. We will use the upper option to do so.
rename length, upper
Now we want to see all the letters of the “LENGTH” variable small. We will use the lower option to do so.
Similarly, we will use the proper option to see the first letter of the “length” variable capital and the other small letters.
rename length, proper
In case, we want to see the first letter of all variable capital and the rest of them small. We will use the proper option, but instead of writing a variable name, we will use the asterisk.
Swapping Variable Names by using Rename Command in Stata
The rename command can also be used to swap the names of variables. For example, we want to generate a new variable named weight. This is going to be the second variable of weight. Therefore, the name assigned to it will be weight2. We want the weight2 equal to 1 if the weight exceeds 3000 pounds. The below command will be used to generate a new variable.
gen weight2=1 if weight>3000
Now we want to name the newly generated variable weight2 as weight. But also want to keep the original weight variable. In that case, we will swap the name of weight2 and weight. First, we will rename the original variable weight as abc using the below command.
rename weight abc
Then we will rename the weight2 as weight.
rename weight2 weight
Lastly, we will rename the abc variable as weight2.
rename abc weight2
The names of the new and original variables are swapped. However, this can be done using a single command written below. This is the simplest way to swap the variables names.
rename (weight weight2) (weight2 weight)
Related Book: Data Management Using Stata by Michael N. Mitchell
Add and Remove Prefix and Suffix
rename command can not only rename variable in Stata, it can also be used to add or remove prefixes and suffixes with the variable’s name. For example, we winsozired variable and now want to keep both the standardized and original variable; in that case, we can add prefixes or suffixes with the name of variables to differentiate the variable. We use the wild card (asterisk) again.
For example, we want to add “car” before all the variables, we will use the prefix, and if we want to add after the variable name, we will use a suffix. We will use the command below to add the “car” prefix.
rename * car_*
To add a suffix, we will use the below command
rename * *_car
In the above command, the asterisk represents all the variable names. You can also add the prefix or suffix to a single variable using the below command.
rename varname prefix_varname rename make car_make rename varname varname_suffix rename make make_car
To remove the prefix and suffix, we will use the below command for a single variable at a time.
rename prefix_varname varname
rename varname_suffix varname
For example, we want to remove the “car” before the variable’s name.
rename car_make make
No, we want to remove the suffix with the variable’s name.
rename make_car make
We will use the below command for all variables at once
rename prefix_* * rename *_suffix *
Add and Remove Prefix and Suffix Variable with Specific Patterns
rename command can also be used to add or remove the prefix or suffix after the variable that has a specific pattern.
Let’s generate a new variable using the below command.
gen length2 = 0
Now, this length2 is not making any sense. We need to interpret whether it’s an original variable or not. So, we add prefixes or suffixes with this variable. Therefore, rather than having 2 at the end of the length, we want the prefix “original.” We can use the below command to do so.
rename *2 *_original
This command shows that any variable that ends with 2 is replaced with the original, rather than 2 at the end. To add the “original” after the name of the variable having 2 at the end, the below command will be used.
rename *2 original_*
Sometimes there is a sequence of variables. In that case, we use #. # is also the same as *. It also has a wild card, but it only selects numbers. Let’s generate three new variables using the below commands.
gen car1=0 gen car111=0 gen carA=0
Now we want to add a prefix after the name of all the variables that have a number at the end. For example, car1 and car111 have numbers at the end. Therefore, we will use # to add the prefix after the names of these two variables.
rename car# =_original
We can also use ?. This ? is also a wild card, but it selects both numbers and alphabets at the end of the variable name.
rename car? =_original