Announcement

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

  • How to rename the full list of variable names?

    I am trying to rename a long list of variable names in several files, without having to write all the variable names every time. Is there a shortcut for doing this? Thank you very much.

  • #2
    Why aren't you writing the rename commands into a do file?

    Comment


    • #3
      I am using the rename command. Then between (), I write the full list of variable names I want to rename, followed by the new list of variable names I want also between (). I am doing this for several files. So I was wondering if there is a shorter way to refer to each file's variable names list, so I don't have to copy all the variables every time and delete all extra spaces manually.
      .

      Comment


      • #4
        That doesn't directly address my question in #2, but let's consider this.

        You can write a file named -rename_myvars.do-, for example. Inside this file, include only the rename command(s). The contents might look like this:

        Code:
        rename oldvar1 newvar1
        rename oldvar2 newvar2
        Note that I am deliberately writing multiple -rename- commands for two reasons. One, it's much easier to troubleshoot and read what is happening. Second, it has conveniences for copying/pasting discussed below.

        Then, in the do-file where you want to perform the renaming, -include- the contents of this file by adding

        Code:
        include rename_myvars.do
        What happens is that when the main do-file is run, it eventually encounters the include statement, and when it finds that file, takes all the contents from -rename_myvars.do- and runs them as if they were inside of the main do-file all along.

        Setting up this way means you can easily call the same file from another do-file if you have exactly the same variables and want to rename them to exactly the same thing. The other thing it does is let you copy/modify contents to a new renaming do-file when dataset structures are somewhat different.

        Comment


        • #5
          Thank you very much. I will do as you explained.

          Comment


          • #6
            Perhaps you are trying to do a systematic rename, such as adding the same prefix to each variable name. Then the output of help rename group suggests that the following example might be what you are looking for.
            Code:
            . ds
            cat    dog    fish   horse
            
            . rename (cat-horse) (my_=)
            
            . ds
            my_cat    my_dog    my_fish   my_horse
            
            .

            Comment

            Working...
            X