Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • rename all variables with lowercase

    Hi,

    I am trying to rename all my variable names to be completely lowercase. Some of them are have them are like this: Blah_blah, others are like this: blah_Blah, etc. I don't want to have to list out every single variable that I want to rename. I have looked through past forum questions, and the following have NOT worked for me:

    foreach var of varlist _all {;
    gen Z=lower(`var');
    drop `var';
    rename Z `var';
    };

    foreach var of varlist _all {;
    local lower = lower(`var');
    rename `var' `lower';
    } ;

    Both tell me "type mismatch."

    Note: I use ; as a delimiter.

    Thanks,
    Alyssa Beavers



  • #2
    Alyssa, you missed the quotes in strlower() function.
    Sergiy

    Code:
    clear
    set more off
    sysuse auto
    describe
    
    foreach var of varlist * {
      rename `var' `=strupper("`var'")'
    }
    
    describe
    
    foreach var of varlist * {
      rename `var' `=strlower("`var'")'
    }
    
    describe
    Last edited by Sergiy Radyakin; 26 Mar 2015, 09:53.

    Comment


    • #3
      why go through a loop at all: rename *, lower

      Comment


      • #4
        Rich Goldstein
        Alyssa mentioned she had variables " Blah_blah, others are like this: blah_Blah"
        I assumed she would be doing some smart renaming in the loop to resolve the name collisions, which appear if rename *, lower encounters variables Blah_blah and blah_Blah
        Code:
        . gen mPG=1
        
        . rename *, lower
        1 new name specified repeatedly in newname
            You requested the name mpg be assigned to 2 existing variables:  mpg and mPG.
        r(198);
        Best, Sergiy Radyakin

        Comment


        • #5
          true, but she uses "_all" in her attempts - not clear to me what she has or wants now

          Comment


          • #6
            Clearly -rename *, lower- is simplest if it will not produce clashes. If it will produce clashes, I would do

            Code:
            foreach v of varlist _all {
                 capture rename `v', lower
                 if c(rc) != 0 {
                     local newname: permname `=lower("`v'")'
                     label var `v' `"RENAME of `v' (`:var label `v'')
                    rename `v' `newname'
                 }
            }

            Comment

            Working...
            X