Renaming a single variable
Renaming a single variable is pretty simple in Stata. Assume that we have the following variables in our data set.
date symbol returns
If we wish to rename the variable ‘returns’ as ret, then the code will be as follows”
rename returns ret
Renaming many variables
We can rename many variables using the “rename group” features of the rename command. So, if we wish to rename all the three variables together, our code will be
rename (date symbol returns) (Date Symbol Returns)
In the above code, we just renamed date to Date, symbol to Symbol, and returns to Returns. From this example, we also learned that Stata’s variables are case sensitive. See the following table for more examples using parentheses.
Code examples
Code | Purpose | Rules |
---|---|---|
ren (a b) (b a) | Swaps a and b | Variable names may be interchanged |
ren (a b c) (b c a) | Renames a to b, b to c, and c to a | There is no limit to how many names may be interchanged |
Rename using a pattern
Change starting letters
Rename all variables starting with firm to Firm
rename firm* Firm*
So if our data has three variables with the names firm1 firm2 firm3
. After the above command, they will be renamed to Firm1 Firm2 Firm3
.
1. *
in old selects the variables to be renamed. *
means that zero or more characters go here.
2. * in new corresponds with * in old and stands for the text that * in old matched.
Removes prefix
Remove the prefix Year from all variables.
rename Year* *
So if our data has three variables with the names Year_data Yearfirm Year_2005
. After the above command, they will be renamed to _data firm _2005
.
Remove suffix
rename Year* *
Let’s assume we have three variables in our data: dataJan, firm_Jan RetJan
. When the command mentioned earlier is executed, these variables will be renamed as data firm Ret
Target by count
If a group of variables have specific number of characters and have a pattern in their names, they call be changed using the ? mark.
rename Jan????? ?????Feb
Let’s assume we have three variables in our data: Jan_2005 Jan_2006 Jan_2007
. When the above command is executed, these variables will be renamed as _2005Feb _2006Feb _2007Feb
. ? means exactly one character, ?? means exactly two characters, etc
Remove middle part
Text enclosed inside ** can be removed from variable names with **
rename *jan* **
So, the above code will rename
janstat to stat, injanstat to instat, and subjan to sub.
Replace middle part
Text enclosed inside *old* can be replace with *old*new. The following code renames all variables that start with jan and contain s to instead end in 1, dropping the jan
rename jan*s* *s*1
So, janstat to stat1 and janest to est1, but not janinc to inc1
Remove given and all following text
rename *jan* *
Remove given and preceding text
Removes jan and whatever precedes it from variable names. For example, midjaninc to inc
rename *jan* .*
Add prefix
Wildcard = in new specifies the original variable name. The following code adds prefix jan to all variables ending in pop. For example, age1pop to janage1pop
rename *pop jan=
Leave A Comment