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 returns variable to just ret, then the code will be

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 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
ren (a b c) (c b a)
Renames a to c and c to a, but leaves b as is Renaming variables to themselves is allowed

Renaming using a pattern

code purpose Rules
rename firm* Firm*
Rename all variables starting firm with Firm 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.
rename jan* *
Removes prefix jan
rename *jan *
Removes suffix jan
rename jan? ?1
removing jan and adding 1 to the end ? means exactly one character, ?? means exactly two characters, etc
rename *jan* **
janstat to stat, injanstat to instat, and subjan to sub You may specify more than one wildcard in old and in new. They correspond in the order given.
rename jan*s* *s*1
Renames all variables that start with jan and contain s to instead end in 1, dropping the jan For example, janstat to stat1 and janest to est1, but not janinc to inc1
rename *jan* *
Removes jan and whatever follows from variable names. For example, renaming statjan to stat, incjan71 to inc You may specify more wildcards in old than in new
rename *jan* .*
Removes jan and whatever precedes it from variable names. For example, midjaninc to inc Wildcard . (dot) in new skips over the corresponding wildcard in old
rename *pop jan=
Adds prefix jan to all variables ending in pop. For example, age1pop to janage1pop Wildcard = in new specifies the original variable name
rename (status bp time) admit=
Renames status to admitstatus, bp to admitbp, and time to admittime