阅读权限 255 威望 13 级论坛币 1100116 个 通用积分 78894.1418 学术水平 3454 点 热心指数 3913 点 信用等级 2749 点 经验 472847 点 帖子 11699 精华 5 在线时间 20305 小时 注册时间 2004-7-15 最后登录 2025-12-21
是的stata的rename的功能增强了不少
多看帮助和manual
Title
[D] rename group -- Rename groups of variables
Syntax
Rename a single variable
rename old new [, options1]
Rename groups of variables
rename (old1 old2 ...) (new1 new2 ...) [, options1]
Change the case of groups of variable names
rename old1 old2 ..., {upper|lower|proper} [options2]
where old and new specify the existing and the new variable names. The rules for specifying them are
1. rename stat status: Renames stat to status.
Rule 1: This is the same rename command documented in [D] rename, with which you are familiar.
2. rename (stat inc) (status income): Renames stat to status and inc to income.
Rule 2: Use parentheses to specify multiple variables for old and new.
3. rename (v1 v2) (v2 v1): Swaps v1 and v2.
Rule 3: Variable names may be interchanged.
4. rename (a b c) (b c a): Swaps names. Renames a to b, b to c, and c to a.
Rule 4: There is no limit to how many names may be interchanged.
5. rename (a b c) (c b a): Renames a to c and c to a, but leaves b as is.
Rule 5: Renaming variables to themselves is allowed.
6. rename jan* *1: Renames all variables starting with jan to instead end with 1, for example, janstat to
stat1, janinc to inc1, etc.
Rule 6.1: * in old selects the variables to be renamed. * means that zero or more characters go here.
Rule 6.2: * in new corresponds with * in old and stands for the text that * in old matched.
* in new or old is called a wildcard character, or just a wildcard.
rename jan* *: Removes prefix jan.
rename *jan *: Removes suffix jan.
7. rename jan? ?1: Renames all variables starting with jan and ending in one character by removing jan and
adding 1 to the end; for example, jans is renamed to s1, but janstat remains unchanged. ? means that
exactly one character goes here, just as * means that zero or more characters go here.
Rule 7: ? means exactly one character, ?? means exactly two characters, etc.
8. rename *jan* **: Removes prefix, midfix, and suffix jan, for example, janstat to stat, injanstat to instat,
and subjan to sub.
Rule 8: 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.
9. rename *jan* *: Removes jan and whatever follows from variable names, thereby renaming statjan to stat,
incjan71 to inc, ....
Rule 9: You may specify more wildcards in old than in new.
10. rename *jan* .*: Removes jan and whatever precedes it from variable names, thereby renaming midjaninc to
inc, ....
Rule 10: Wildcard . (dot) in new skips over the corresponding wildcard in old.
11. rename *pop jan=: Adds prefix jan to all variables ending in pop, for example, age1pop to janage1pop, ....
rename (status bp time) admit=: Renames status to admitstatus, bp to admitbp, and time to admittime.
rename whatever pre=: Adds prefix pre to all variables selected by whatever, however whatever is specified.
Rule 11: Wildcard = in new specifies the original variable name.
rename whatever =jan: Adds suffix jan to all variables selected by whatever.
rename whatever pre=fix: Adds prefix pre and suffix fix to all variables selected by whatever.
12. rename v# stat#: Renames v1 to stat1, v2 to stat2, ..., v10 to stat10, ....
Rule 12.1: # is like * but for digits. # in old selects one or more digits.
Rule 12.2: # in new copies the digits just as they appear in the corresponding old.
13. rename v(#) stat(#): Renames v1 to stat1, v2 to stat2, ..., but does not rename v10, ....
Rule 13.1: (#) in old selects exactly one digit. Similarly, (##) selects exactly two digits, and so
on, up to ten # symbols.
Rule 13.2: (#) in new means reformat to one or more digits. Similarly, (##) reformats to two or more
digits, and so on, up to ten # symbols.
rename v(##) stat(##): Renames v01 to stat01, v02 to stat02, ..., v10 to stat10, ..., but does not rename
v0, v1, v2, ..., v9, v100, ....
14. rename v# v(##): Renames v1 to v01, v2 to v02, ..., v10 to v10, v11 to v11, ..., v100 to v100, v101 to
v101, ....
Rule 14: You may combine #, (#), (##), ... in old with any of #, (#), (##), ... in new.
rename v(##) v(#): Renames v01 to v1, v02 to v2, ..., v10 to v10, ..., but does not rename v001, etc.
rename stat(##) stat_20(##): Renames stat10 to stat_2010, stat11 to stat_2011, ..., but does not rename
stat1, stat2, ....
rename stat(#) to stat_200(#): Renames stat1 to stat_2001, stat2 to stat_2002, ..., but does not rename
stat10 or stat_2010.
15. rename v# (a b c): Renames v1 to a, v10 to b, and v2 to c if variables v1, v10, v2 appear in that order in
the data. Because three variables were specified in new, v# in old must select three variables or rename
will issue an error.
Rule 15.1: You may mix syntaxes. Note that the explicit and implied numbers of variables must agree.
rename v# (a b c), sort: Renames (for instance) v1 to a, v2 to b, and v10 to c.
Rule 15.2: The sort option places the variables selected by old in order and does so smartly. In the
case where #, (#), (##), ... appear in old, sort places the variables in numeric order.
rename v* (a b c), sort: Renames (for instance) valpha to a, vbeta to b, and vgamma to c regardless of the
order of the variables in the data.
Rule 15.3: In the case where * or ? appears in old, sort places the variables in alphabetical order.
16. rename v# v#, renumber: Renames (for instance) v9 to v1, v10 to v2, v8 to v3, ..., assuming that variables
v9, v10, v8, ... appear in that order in the data.
Rule 16.1: The renumber option resequences the numbers.
rename v# v#, renumber sort: Renames (for instance) v8 to v1, v9 to v2, v10 to v3, .... Concerning option
sort, see rule 15.2 above.
rename v# v#, renumber(10) sort: Renames (for instance) v8 to v10, v9 to v11, v10 to v12, ....
Rule 16.2: The renumber(#) option allows you to specify the starting value.
17. rename v* v#, renumber: Renames (for instance) valpha to v1, vgamma to v2, vbeta to v3, ..., assuming
variables valpha, vgamma, vbeta, ... appear in that order in the data.
Rule 17: # in new may correspond to *, ?, #, (#), (##), ... in old.
rename v* v#, renumber sort: Renames (for instance) valpha to v1, vbeta to v2, vgamma to v3, .... Also see
rule 15.3 above concerning the sort option.
rename *stat stat#, renumber: Renames, for instance, janstat to stat1, febstat to stat2, .... Note that #
in new corresponds to * in old, just as in the previous example.
rename *stat stat(##), renumber: Renames, for instance, janstat to stat01, febstat to stat02, ....
rename *stat stat#, renumber(0): Renames, for instance, janstat to stat0, febstat to stat1, ....
rename *stat stat#, renumber sort: Renames, for instance, aprstat to stat1, augstat to stat2, ....
18. rename (a b c) v#, addnumber: Renames a to v1, b to v2, and c to v3.
Rule 18: The addnumber option allows you to add numbering. More formally, if you specify addnumber,
you may specify one more wildcard in new than is specified in old, and that extra wildcard must be #,
(#), (##), ....
19. rename a(#)(#) a(#)[2](#)[1]: Renames a12 to a21, a13 to a31, a14 to a41, ..., a21 to a12, ....
Rule 19.1: You may specify explicit subscripts with wildcards in new to make explicit its matching
wildcard in old. Subscripts are specified in square brackets after a wildcard in new. The number
refers to the number of the wildcard in old.
rename *stat* *[2]stat*[1]: Swaps prefixes and suffixes; it renames bpstata to astatbp, rstater to erstatr,
etc.
rename *stat* *[2]stat*: Does the same as above; it swaps prefixes and suffixes.
Rule 19.2: After specifying a subscripted wildcard, subsequent unsubscripted wildcards correspond to
the same wildcards in old as they would if you had removed the subscripted wildcards altogether.
rename v#a# v#_#[1]_a#[2]: Renames v1a1 to v1_1_a1, v1a2 to v1_1_a2, ..., v2a1 to v2_2_a1, ....
Rule 19.3: Using subscripts, you may refer to the same wildcard in old more than once.
Subscripts are commonly used to interchange suffixes at the ends of variable names. For instance, you have
districts and schools within them, and many of the variable names in your data match *_#_#. The first
number records district and the second records school within district. To reverse the ordering, you type
rename *_#_# *_#[3]_#[2]. When specifying subscripts, you refer to them by the position number in the
original name. For example, our original name was *_#_# so [1] refers to *, [2] refers to the first #, and
[3] refers to the last #.
In summary, the pattern specifiers are
Specifier Meaning in old
-------------------------------------------------------------------
* 0 or more characters
? 1 character exactly
# 1 or more digits
(#) 1 digit exactly
(##) 2 digits exactly
(###) 3 digits exactly
...
(##########) 10 digits exactly
-------------------------------------------------------------------
May correspond
Specifier in old with Meaning in new
-------------------------------------------------------------------
* *, ?, #, (#), ... copies matched text
? ? copies a character
# #, (#), ... copies a number as is
(#) #, (#), ... reformats to 1 or more digits
(##) #, (#), ... reformats to 2 or more digits
...
(##########) #, (#), ... reformats to 10 digits
. *, ?, #, (#), ... skip
= nothing copies entire variable name
-------------------------------------------------------------------
Specifier # in any of its guises may also correspond with * or ? if the renumber option is specified.
The options are as follows:
options1 Description
--------------------------------------------------------------------------------------------------------------
addnumber add sequential numbering to end
addnumber(#) addnumber, starting at #
renumber renumber sequentially
renumber(#) renumber, starting at #
sort sort before numbering
dryrun do not rename, but instead produce a report
r save variable names in r() for programming use
--------------------------------------------------------------------------------------------------------------
These options correspond to the first and second syntaxes.
options2 Description
--------------------------------------------------------------------------------------------------------------
upper uppercase variable names (UPPERCASE)
lower lowercase variable names (lowercase)
proper propercase variable names (Propercase)
dryrun do not rename, but instead produce a report
r save variable names in r() for programming use
--------------------------------------------------------------------------------------------------------------
These options correspond to the third syntax. One of upper, lower, or proper must be specified.
Menu
Data > Data utilities > Rename variables
Description
rename changes the names of existing variables to the new names specified. See [D] rename for the base rename
syntax. Documented here is the advanced syntax for renaming groups of variables.
Options for renaming variables
addnumber and addnumber(#) specify to add a sequence number to the variable names. See item 18 of Syntax. If #
is not specified, the sequence number begins with 1.
renumber and renumber(#) specify to replace existing numbers or text in a set of variable names with a sequence
number. See items 16 and 17 of Syntax. If # is not specified, the sequence number begins with 1.
sort specifies that the existing names be placed in order before the renaming is performed. See item 15 of Syntax
for details. This ordering matters only when addnumber or renumber is also specified or when specifying a
list of variable names for old or new.
dryrun specifies that the requested renaming not be performed but instead that a table be displayed showing the
old and new variable names. It is often a good idea to specify this option before actually renaming the
variables.
r is a programmer's option that requests that old and new variable names be saved in r(). This option may be
specified with or without dryrun.
Options for changing the case of groups of variable names
upper, lower, and proper specify how the variables are to be renamed. upper specifies that variable names be
changed to uppercase; lower, to lowercase; and proper, to having the first letter capitalized and the
remaining letters in lowercase. One of these three options must be specified.
dryrun and r are the same options as documented directly above.
Saved results
rename saves nothing in r() by default. If the r option is specified, then rename saves the following in r():
Scalars
r(r) number of variables to be renamed
Macros
r(oldnames) original variable names
r(newnames) new variable names
Variables that are renamed to themselves are omitted from the recorded lists. 复制代码