Announcement

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

  • Rename variables - substr

    Dear users,

    I have a list of variables that I would like to rename.
    I would like to modify the variable names by applying the following changes to the original variable names:

    - Remove all characters before "x"
    - Add "knows" at the beginning of each variable.
    - Replace the symbol "_" by the symbol "-" .

    Here's an example of what I would like to accomplish with this set of variables.

    - Old variable_name : Q3_8_x1_1
    - New variable name: knows1-1

    In the original variable names, the first substring of the variable is common to all variables (Q3_8_x), and the second part changes for each variable, ranging from 1_1, 1_2.....to 10_10.

    I made an attempt to create a loop, but it's incomplete.
    I get an error, and I'm not sure on how to code the replacement of "_" by the symbol "-".

    Code:
    foreach v of varlist Q3_8_x1_1 - Q3_8_x10_10 {
    
        local new = substr("`v'",strpos(`v',"x"),5)
        rename `v' knows`new'
    }
    Thanks a lot for the help.

  • #2
    Originally posted by Oscar Ll View Post
    I would like to modify the variable names by applying the following changes to the original variable names:

    [...]
    - Replace the symbol "_" by the symbol "-" .

    [...] I'm not sure on how to code the replacement of "_" by the symbol "-".
    That is not possible. Stata does not allow the hyphen/minus-sign in variable names; it indicates a range of variables, if you type

    Code:
    varname-varname
    If hyphens/minus-signs were allowed in variable names, Stata would have no way of knowing whether you are referring to one variable or a range of variables.

    Setting the hyphen/minus-sign aside

    Code:
    rename *x* knowsx*[2]
    does what you want.

    Best
    Daniel

    Comment


    • #3
      Thanks, Daniel.

      Based on your suggestion, I tried the following:

      Code:
      foreach v of varlist Q3_8_x1_1 - Q3_8_x10_10 {
      
      rename `v' knowsx*[2]
      }
      But I get an error message:

      Code:
      new pattern invalid
          You specified old = Q3_8_x1_1 and new = knowsx*[2].  There is no 2nd wildcard in old.
      Thanks again.

      Comment


      • #4
        Your trial didn't match Daniel Klein's pattern. You should try

        Code:
        rename (Q3_8_x1_1 - Q3_8_x10_10) knowsx*[2]

        Comment


        • #5
          Originally posted by Nick Cox View Post
          You should try
          Code:
          rename (Q3_8_x1_1 - Q3_8_x10_10) knowsx*[2]
          That won't work, either. You cannot refer to a wildcard in newnames if you have not used one in oldnames. The code that I have suggested should be typed as-is: no loop; two wildcards in oldnames.

          Edit: For readability, my original suggestion should be changed to

          Code:
          rename (*x*) (knowsx*[2])
          Best
          Daniel
          Last edited by daniel klein; 05 Jul 2019, 11:52.

          Comment


          • #6
            Thanks both for the suggestions.
            Daniel's code works perfectly.

            Best,
            Oscar

            Comment


            • #7
              Nick Cox Hi Nick what about if I want to rename prob_q3ashockey_OPA to prob_q3ashockey_UPA

              Thank you.

              Comment


              • #8
                Code:
                rename prob_q3ashockey_OPA  prob_q3ashockey_UPA
                Not sure what you have in mind here.

                Comment


                • #9
                  Originally posted by Nick Cox View Post
                  Code:
                  rename prob_q3ashockey_OPA prob_q3ashockey_UPA
                  Not sure what you have in mind here.
                  I have a lot of variables that start by prob_ and finish by OPA that I want to change their suffix only from OPA to UPA.

                  Last edited by Radhouene DOGGUI; 18 Sep 2020, 10:35.

                  Comment


                  • #10
                    Code:
                    rename (prob_*_OPA) (prob_*_UPA)

                    Comment


                    • #11
                      Check

                      help rename group

                      and if what you want to do does not fall into one of the examples there, put a sample of your data using -dataex-.

                      Originally posted by Radhouene DOGGUI View Post

                      I have a lot of variables that start by prob_ and finish by OPA that I want to change their suffix only from OPA to UPA.

                      Comment

                      Working...
                      X