Announcement

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

  • renaming a group of variables using rename

    Hi All

    I have a set of variables created during imputation as follows:

    _1_rutter3all
    _2_rutter3all
    _3_rutter3all
    _4_rutter3all
    _5_rutter3all
    _6_rutter3all
    _7_rutter3all
    _8_rutter3all
    _9_rutter3all
    _10_rutter3all
    _11_rutter3all
    _12_rutter3all
    _13_rutter3all
    _14_rutter3all
    _15_rutter3all
    _16_rutter3all
    _17_rutter3all
    _18_rutter3all
    _19_rutter3all
    _20_rutter3all
    _21_rutter3all
    _22_rutter3all
    _23_rutter3all
    _24_rutter3all
    _25_rutter3all

    I would like to rename the above variables by dropping the suffix 'all' (but keeping all other parts of the variable name). So, the new names will read: _1_rutter3, _2_rutter3, _3_rutter3 and so on.

    I tried this:

    Code:
    rename (_1_rutter3all-_25_rutter3all) (*all* **)
    However, I get an error message. So, I'm obviously typing something wrong but I can't figure out what it is. Any tips?

    Many Thanks
    /Amal

  • #2
    Code:
    rename *all *

    Comment


    • #3
      You should probably either use mi rename or re-register your imputed variables appropriately after re-naming. With mi data, even trivial data-management tools can have adverse side-effects.

      Comment


      • #4
        Thanks but I didn't understand your code above. Did you mean:

        Code:
        rename (_1_rutter3all-_25_rutter3all) (*all *)
        The above and just typing
        Code:
        rename *all *
        doesn't work.....

        Thanks
        /Amal

        Comment


        • #5
          Well, this is interesting. I cannot quickly find it documented, but rename appears to omit variables beginning with "_" from variable lists created with a wildcard "*" at the front, so the command you need is
          Code:
          rename _*all _*
          And a note to improve your future posts. Telling us you got an error message without telling us what the error message was does not take much less typing than, and is not as helpful as, copying and patsing the command and its output into your post.
          Code:
          . rename *all *
            (nothing to rename)
          tells me that "*all" was not matched to anything by rename.

          Comment


          • #6
            Originally posted by William Lisowski View Post
            Well, this is interesting. I cannot quickly find it documented, but rename appears to omit variables beginning with "_" from variable lists created with a wildcard "*" at the front.
            I have no quick reference either, but this might actually turn out to be a safety feature because variables that start with an underscore are usually system variables, created by Stata. It is usually a bad idea to mess with such variables. So, no offense here, but I doubt that William's quick workaround will prove to be much of a help when Amal manages to mess up his mi system variables ...
            Last edited by daniel klein; 06 Jul 2020, 11:12.

            Comment


            • #7
              Still no reference, but this is from rename

              Code:
              *! version 1.0.3  02feb2012

              Code:
                              /* remove _vars if appropriate        */
                  if (!filter_varlist) {
                      if (length(oldp.ellist)) {
                          tt = oldp.ellist[1].elementtype
                          if (tt==`ETstar' | tt==`ETqm') {
                              oldp.varnames = select(oldp.varnames,
                                      substr(oldp.varnames,1,1):!="_")
                              nvars         = length(oldp.varnames)
                          }
                      }
                  }
              This code suggests that the behavior in question is definitely intended.

              Comment


              • #8
                I agree entirely with the point made by daniel klein in posts #3 and #5, and in haste neglected to cite that advice in my post #5. I tried to write what Daniel did in post #6 but wasn't having luck finding the words, so my answer went up with no acknowledgement that it is the right answer to what is in this case the wrong question. If "all" was added by mi then you should not change it; if it was something prior to mi, you should change it prior to running mi.

                Comment


                • #9
                  It's germane, I suspect, that all names of temporary variables start with double underscore.

                  Comment


                  • #10
                    Originally posted by Nick Cox View Post
                    It's germane, I suspect, that all names of temporary variables start with double underscore.
                    Very good point; I did not even think of this. However, if StataCorp aimed at omitting temporary variables only, why not looking for

                    Code:
                    substr(oldp.varnames,1,2):!="__")
                    My guess is that they actually did go for both temporary variables and system variables.


                    This is getting off-topic but is there a way to get a list of temporary variables other than checking for two underscores (followed by six characters)?

                    Comment


                    • #11
                      I may be missing some key information since many of the responses are discussing mi, which I am not familiar with. But in the case that the original question is as straight-forward as it seems, this code will do what you ask

                      Code:
                      forval x=1/25{
                      rename _`x'_rutter3all _`x'_rutter3
                      }

                      Comment


                      • #12
                        Posts #3, #6, and #8 agree that renaming these variables, which post #1 tells us were created as part of a mi (multiple imputation) process, is not straightforward since it will almost certainly adversely affect the function of subsequent mi commands that rely on them.

                        Post #5 presents a one-line rename command that does not require looping. As I acknowledged in post #8, it provides an answer for what turns out to be an inappropriate question. For details on easily renainge a group of variables systematically, see the output of help rename group for extensive details on these powerful advanced features of the rename command.
                        Last edited by William Lisowski; 06 Jul 2020, 15:06.

                        Comment

                        Working...
                        X