楼主: honestwh
4513 5

stata如何在很多变量名后统一加上2002的简便方法,谢谢 [推广有奖]

  • 8关注
  • 8粉丝

已卖:1929份资源

教授

82%

还不是VIP/贵宾

-

威望
0
论坛币
83272 个
通用积分
50.0425
学术水平
0 点
热心指数
7 点
信用等级
0 点
经验
15374 点
帖子
268
精华
0
在线时间
2777 小时
注册时间
2005-9-25
最后登录
2025-12-15

楼主
honestwh 发表于 2012-1-18 08:12:28 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
stata如何在很多变量名后统一加上2002的简便方法,谢谢
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Stata tata 多变量 统一 如何

沙发
liuxb 发表于 2012-1-18 12:18:31
如果你用的是stata12,用这个命令:
rename * =2002

藤椅
znxkxx 发表于 2012-1-18 22:44:44
use the command "rename".

板凳
kirin_guess 发表于 2012-1-30 01:57:55
If you have 5 variables, x1, x2, x3, x4, x5, and you want to change add "2002" to the first three variables, but let the other two unchanged. Then, you can use the following syntax:
-----------------------------------.
local ivlist x1 x2 x3
local n: word count `ivlist'
tokenize `ivlist'
forvalues i = 1/`n' {
   rename ``i'' ``i''2002
   }
.
-----------------------------------.

报纸
znxkxx 发表于 2012-2-1 13:31:44
in stata 12 try this command "rename * *2002", and you will get a surprise!

地板
蓝色 发表于 2012-2-1 19:37:35
是的stata的rename的功能增强了不少
多看帮助和manual


  1. Title

  2. [D] rename group -- Rename groups of variables


  3. Syntax

  4. Rename a single variable

  5. rename old new [, options1]


  6. Rename groups of variables

  7. rename (old1 old2 ...) (new1 new2 ...) [, options1]


  8. Change the case of groups of variable names

  9. rename old1 old2 ..., {upper|lower|proper} [options2]


  10. where old and new specify the existing and the new variable names. The rules for specifying them are

  11. 1. rename stat status: Renames stat to status.

  12. Rule 1: This is the same rename command documented in [D] rename, with which you are familiar.

  13. 2. rename (stat inc) (status income): Renames stat to status and inc to income.

  14. Rule 2: Use parentheses to specify multiple variables for old and new.

  15. 3. rename (v1 v2) (v2 v1): Swaps v1 and v2.

  16. Rule 3: Variable names may be interchanged.

  17. 4. rename (a b c) (b c a): Swaps names. Renames a to b, b to c, and c to a.

  18. Rule 4: There is no limit to how many names may be interchanged.

  19. 5. rename (a b c) (c b a): Renames a to c and c to a, but leaves b as is.

  20. Rule 5: Renaming variables to themselves is allowed.

  21. 6. rename jan* *1: Renames all variables starting with jan to instead end with 1, for example, janstat to
  22. stat1, janinc to inc1, etc.

  23. Rule 6.1: * in old selects the variables to be renamed. * means that zero or more characters go here.

  24. Rule 6.2: * in new corresponds with * in old and stands for the text that * in old matched.

  25. * in new or old is called a wildcard character, or just a wildcard.

  26. rename jan* *: Removes prefix jan.

  27. rename *jan *: Removes suffix jan.


  28. 7. rename jan? ?1: Renames all variables starting with jan and ending in one character by removing jan and
  29. adding 1 to the end; for example, jans is renamed to s1, but janstat remains unchanged. ? means that
  30. exactly one character goes here, just as * means that zero or more characters go here.

  31. Rule 7: ? means exactly one character, ?? means exactly two characters, etc.

  32. 8. rename *jan* **: Removes prefix, midfix, and suffix jan, for example, janstat to stat, injanstat to instat,
  33. and subjan to sub.

  34. Rule 8: You may specify more than one wildcard in old and in new. They correspond in the order given.

  35. rename jan*s* *s*1: Renames all variables that start with jan and contain s to instead end in 1, dropping
  36. the jan, for example, janstat to stat1 and janest to est1, but not janinc to inc1.

  37. 9. rename *jan* *: Removes jan and whatever follows from variable names, thereby renaming statjan to stat,
  38. incjan71 to inc, ....

  39. Rule 9: You may specify more wildcards in old than in new.

  40. 10. rename *jan* .*: Removes jan and whatever precedes it from variable names, thereby renaming midjaninc to
  41. inc, ....

  42. Rule 10: Wildcard . (dot) in new skips over the corresponding wildcard in old.

  43. 11. rename *pop jan=: Adds prefix jan to all variables ending in pop, for example, age1pop to janage1pop, ....

  44. rename (status bp time) admit=: Renames status to admitstatus, bp to admitbp, and time to admittime.

  45. rename whatever pre=: Adds prefix pre to all variables selected by whatever, however whatever is specified.

  46. Rule 11: Wildcard = in new specifies the original variable name.

  47. rename whatever =jan: Adds suffix jan to all variables selected by whatever.

  48. rename whatever pre=fix: Adds prefix pre and suffix fix to all variables selected by whatever.

  49. 12. rename v# stat#: Renames v1 to stat1, v2 to stat2, ..., v10 to stat10, ....

  50. Rule 12.1: # is like * but for digits. # in old selects one or more digits.

  51. Rule 12.2: # in new copies the digits just as they appear in the corresponding old.

  52. 13. rename v(#) stat(#): Renames v1 to stat1, v2 to stat2, ..., but does not rename v10, ....

  53. Rule 13.1: (#) in old selects exactly one digit. Similarly, (##) selects exactly two digits, and so
  54. on, up to ten # symbols.

  55. Rule 13.2: (#) in new means reformat to one or more digits. Similarly, (##) reformats to two or more
  56. digits, and so on, up to ten # symbols.

  57. rename v(##) stat(##): Renames v01 to stat01, v02 to stat02, ..., v10 to stat10, ..., but does not rename
  58. v0, v1, v2, ..., v9, v100, ....

  59. 14. rename v# v(##): Renames v1 to v01, v2 to v02, ..., v10 to v10, v11 to v11, ..., v100 to v100, v101 to
  60. v101, ....

  61. Rule 14: You may combine #, (#), (##), ... in old with any of #, (#), (##), ... in new.

  62. rename v(##) v(#): Renames v01 to v1, v02 to v2, ..., v10 to v10, ..., but does not rename v001, etc.

  63. rename stat(##) stat_20(##): Renames stat10 to stat_2010, stat11 to stat_2011, ..., but does not rename
  64. stat1, stat2, ....

  65. rename stat(#) to stat_200(#): Renames stat1 to stat_2001, stat2 to stat_2002, ..., but does not rename
  66. stat10 or stat_2010.

  67. 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
  68. the data. Because three variables were specified in new, v# in old must select three variables or rename
  69. will issue an error.

  70. Rule 15.1: You may mix syntaxes. Note that the explicit and implied numbers of variables must agree.

  71. rename v# (a b c), sort: Renames (for instance) v1 to a, v2 to b, and v10 to c.

  72. Rule 15.2: The sort option places the variables selected by old in order and does so smartly. In the
  73. case where #, (#), (##), ... appear in old, sort places the variables in numeric order.

  74. rename v* (a b c), sort: Renames (for instance) valpha to a, vbeta to b, and vgamma to c regardless of the
  75. order of the variables in the data.

  76. Rule 15.3: In the case where * or ? appears in old, sort places the variables in alphabetical order.

  77. 16. rename v# v#, renumber: Renames (for instance) v9 to v1, v10 to v2, v8 to v3, ..., assuming that variables
  78. v9, v10, v8, ... appear in that order in the data.

  79. Rule 16.1: The renumber option resequences the numbers.

  80. rename v# v#, renumber sort: Renames (for instance) v8 to v1, v9 to v2, v10 to v3, .... Concerning option
  81. sort, see rule 15.2 above.

  82. rename v# v#, renumber(10) sort: Renames (for instance) v8 to v10, v9 to v11, v10 to v12, ....

  83. Rule 16.2: The renumber(#) option allows you to specify the starting value.

  84. 17. rename v* v#, renumber: Renames (for instance) valpha to v1, vgamma to v2, vbeta to v3, ..., assuming
  85. variables valpha, vgamma, vbeta, ... appear in that order in the data.

  86. Rule 17: # in new may correspond to *, ?, #, (#), (##), ... in old.

  87. rename v* v#, renumber sort: Renames (for instance) valpha to v1, vbeta to v2, vgamma to v3, .... Also see
  88. rule 15.3 above concerning the sort option.

  89. rename *stat stat#, renumber: Renames, for instance, janstat to stat1, febstat to stat2, .... Note that #
  90. in new corresponds to * in old, just as in the previous example.

  91. rename *stat stat(##), renumber: Renames, for instance, janstat to stat01, febstat to stat02, ....

  92. rename *stat stat#, renumber(0): Renames, for instance, janstat to stat0, febstat to stat1, ....

  93. rename *stat stat#, renumber sort: Renames, for instance, aprstat to stat1, augstat to stat2, ....

  94. 18. rename (a b c) v#, addnumber: Renames a to v1, b to v2, and c to v3.

  95. Rule 18: The addnumber option allows you to add numbering. More formally, if you specify addnumber,
  96. you may specify one more wildcard in new than is specified in old, and that extra wildcard must be #,
  97. (#), (##), ....

  98. 19. rename a(#)(#) a(#)[2](#)[1]: Renames a12 to a21, a13 to a31, a14 to a41, ..., a21 to a12, ....

  99. Rule 19.1: You may specify explicit subscripts with wildcards in new to make explicit its matching
  100. wildcard in old. Subscripts are specified in square brackets after a wildcard in new. The number
  101. refers to the number of the wildcard in old.

  102. rename *stat* *[2]stat*[1]: Swaps prefixes and suffixes; it renames bpstata to astatbp, rstater to erstatr,
  103. etc.

  104. rename *stat* *[2]stat*: Does the same as above; it swaps prefixes and suffixes.

  105. Rule 19.2: After specifying a subscripted wildcard, subsequent unsubscripted wildcards correspond to
  106. the same wildcards in old as they would if you had removed the subscripted wildcards altogether.

  107. rename v#a# v#_#[1]_a#[2]: Renames v1a1 to v1_1_a1, v1a2 to v1_1_a2, ..., v2a1 to v2_2_a1, ....

  108. Rule 19.3: Using subscripts, you may refer to the same wildcard in old more than once.

  109. Subscripts are commonly used to interchange suffixes at the ends of variable names. For instance, you have
  110. districts and schools within them, and many of the variable names in your data match *_#_#. The first
  111. number records district and the second records school within district. To reverse the ordering, you type
  112. rename *_#_# *_#[3]_#[2]. When specifying subscripts, you refer to them by the position number in the
  113. original name. For example, our original name was *_#_# so [1] refers to *, [2] refers to the first #, and
  114. [3] refers to the last #.

  115. In summary, the pattern specifiers are

  116. Specifier Meaning in old
  117. -------------------------------------------------------------------
  118. * 0 or more characters
  119. ? 1 character exactly
  120. # 1 or more digits
  121. (#) 1 digit exactly
  122. (##) 2 digits exactly
  123. (###) 3 digits exactly
  124. ...
  125. (##########) 10 digits exactly
  126. -------------------------------------------------------------------

  127. May correspond
  128. Specifier in old with Meaning in new
  129. -------------------------------------------------------------------
  130. * *, ?, #, (#), ... copies matched text
  131. ? ? copies a character
  132. # #, (#), ... copies a number as is
  133. (#) #, (#), ... reformats to 1 or more digits
  134. (##) #, (#), ... reformats to 2 or more digits
  135. ...
  136. (##########) #, (#), ... reformats to 10 digits
  137. . *, ?, #, (#), ... skip
  138. = nothing copies entire variable name
  139. -------------------------------------------------------------------
  140. Specifier # in any of its guises may also correspond with * or ? if the renumber option is specified.

  141. The options are as follows:

  142. options1 Description
  143. --------------------------------------------------------------------------------------------------------------
  144. addnumber add sequential numbering to end
  145. addnumber(#) addnumber, starting at #
  146. renumber renumber sequentially
  147. renumber(#) renumber, starting at #
  148. sort sort before numbering

  149. dryrun do not rename, but instead produce a report
  150. r save variable names in r() for programming use
  151. --------------------------------------------------------------------------------------------------------------
  152. These options correspond to the first and second syntaxes.

  153. options2 Description
  154. --------------------------------------------------------------------------------------------------------------
  155. upper uppercase variable names (UPPERCASE)
  156. lower lowercase variable names (lowercase)
  157. proper propercase variable names (Propercase)

  158. dryrun do not rename, but instead produce a report
  159. r save variable names in r() for programming use
  160. --------------------------------------------------------------------------------------------------------------
  161. These options correspond to the third syntax. One of upper, lower, or proper must be specified.


  162. Menu

  163. Data > Data utilities > Rename variables


  164. Description

  165. rename changes the names of existing variables to the new names specified. See [D] rename for the base rename
  166. syntax. Documented here is the advanced syntax for renaming groups of variables.


  167. Options for renaming variables

  168. addnumber and addnumber(#) specify to add a sequence number to the variable names. See item 18 of Syntax. If #
  169. is not specified, the sequence number begins with 1.

  170. renumber and renumber(#) specify to replace existing numbers or text in a set of variable names with a sequence
  171. number. See items 16 and 17 of Syntax. If # is not specified, the sequence number begins with 1.

  172. sort specifies that the existing names be placed in order before the renaming is performed. See item 15 of Syntax
  173. for details. This ordering matters only when addnumber or renumber is also specified or when specifying a
  174. list of variable names for old or new.

  175. dryrun specifies that the requested renaming not be performed but instead that a table be displayed showing the
  176. old and new variable names. It is often a good idea to specify this option before actually renaming the
  177. variables.

  178. r is a programmer's option that requests that old and new variable names be saved in r(). This option may be
  179. specified with or without dryrun.


  180. Options for changing the case of groups of variable names

  181. upper, lower, and proper specify how the variables are to be renamed. upper specifies that variable names be
  182. changed to uppercase; lower, to lowercase; and proper, to having the first letter capitalized and the
  183. remaining letters in lowercase. One of these three options must be specified.

  184. dryrun and r are the same options as documented directly above.


  185. Saved results

  186. rename saves nothing in r() by default. If the r option is specified, then rename saves the following in r():

  187. Scalars
  188. r(r) number of variables to be renamed

  189. Macros
  190. r(oldnames) original variable names
  191. r(newnames) new variable names

  192. Variables that are renamed to themselves are omitted from the recorded lists.
复制代码

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-21 14:15