楼主: peyzf
18312 9

[数据管理求助] 如何将数据变量名中的大写批量地改为小写? [推广有奖]

  • 1关注
  • 63粉丝

警督

大师

59%

还不是VIP/贵宾

-

威望
2
论坛币
566901 个
通用积分
188.0557
学术水平
218 点
热心指数
240 点
信用等级
140 点
经验
132143 点
帖子
12795
精华
0
在线时间
2969 小时
注册时间
2007-9-8
最后登录
2024-4-24

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
由于stata区分大小写,如何将大写的变量名统一修改为小写?
二维码

扫码加我 拉你入群

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

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

关键词:Stata 区分大小写 tata 大小写 如何

本帖被以下文库推荐

沙发
tony2040044 发表于 2015-3-2 21:12:19 |只看作者 |坛友微信交流群
放到word或excel中 shift+F3

或者你用 ren 一个一个替换呗

使用道具

藤椅
蓝色 发表于 2015-3-2 21:22:38 |只看作者 |坛友微信交流群
看rename的帮助或manual的介绍

可以实现的
已有 1 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
hustchen2012 + 10 + 10 + 2 + 2 + 2 热心帮助其他会员

总评分: 经验 + 10  论坛币 + 10  学术水平 + 2  热心指数 + 2  信用等级 + 2   查看全部评分

使用道具

板凳
peyzf 发表于 2015-3-3 00:12:32 |只看作者 |坛友微信交流群
谢谢。如果能够在stata中批量实现为佳。

使用道具

报纸
voodoo 发表于 2015-3-3 08:14:09 |只看作者 |坛友微信交流群
1. help rename group,具体实现为rename *, lower
2. help import:import excel或import delimited均有个case(preserve|lower|upper)选项,在导入时直接就转换为小写





已有 4 人评分学术水平 热心指数 收起 理由
hg2019100 + 1 + 1 精彩帖子
你不要走 + 1 + 1 精彩帖子
peyzf + 1 + 1 good suggestion.
蓝色 + 3 + 3 热心帮助其他会员

总评分: 学术水平 + 6  热心指数 + 6   查看全部评分

使用道具

地板
蓝色 发表于 2015-3-3 08:32:27 |只看作者 |坛友微信交流群
voodoo 发表于 2015-3-3 08:14
1. help rename group,具体实现为rename *, lower
2. help import:import excel或import delimited均有个 ...
许多求助的人就是不看help和manual的, user guide和data  management 两本看看也花不了多长时间的




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                    store 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                    store variable names in r() for programming use
        -------------------------------------------------------------------------------------------------------------------------------------------------
        These options correspond to the third syntax.  One of upper, lower, or proper must be specified.


已有 1 人评分学术水平 热心指数 收起 理由
peyzf + 1 + 1 精彩帖子

总评分: 学术水平 + 1  热心指数 + 1   查看全部评分

使用道具

7
peyzf 发表于 2015-3-3 09:41:37 |只看作者 |坛友微信交流群
wow, cool~
stata is powerful~

使用道具

8
voodoo 发表于 2015-3-3 11:00:10 |只看作者 |坛友微信交流群
蓝色 发表于 2015-3-3 08:32
许多求助的人就是不看help和manual的, user guide和data  management 两本看看也花不了多长时间的


所言甚是!但这应该是Stata12才出现的rename新功能,也怪不得大家啦。:-)










使用道具

9
馨雨初缘 学生认证  发表于 2020-8-11 23:55:37 |只看作者 |坛友微信交流群
怎么把stata里面的标签全部改成小写呀

使用道具

10
Jennywis 发表于 2023-6-13 17:56:27 |只看作者 |坛友微信交流群
rename _all, lower

同理,变为大写如下:
rename _all, upper
所有变量一次性解决

使用道具

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

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-30 17:25