Announcement

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

  • Renaming all variables in a dataset at once according to a list

    I have a dataset where every variable name is a long string (basically the first 32 characters of the associated survey question). I have a corresponding list of simplified variable names that are, importantly, in the same order. I want to rename all variables to their associated name in the simplified list.

    The long way to do this in stata is:

    rename longstring1 simplifiedname1
    rename longstring2 simplifiedname2
    .... and so on

    In the past, I have avoided this by importing the dataset to excel, and simply replacing the first row with the simplified names by copying and pasting that row from an excel sheet where I have the simplified names stored. Then I just save that excel sheet and import that into stata.

    Is there a way that I can do this process in stata without renaming every variable one by one?

  • #2
    Code:
    rename (*) (whatever something anotherthing)
    is perfectly legal and I use similar commands frequently. It's hard to know about your practicalities for bigger datasets:

    * I've not done any tests to compare the speed of several rename commands compared with far fewer, e.g. one,

    * A really long list of new names would be harder to edit and could lead to horrible bugs or messages about errors that are hard to fix quickly.

    * Your names are already in MS Excel, I would consider putting them in a Stata .do file, as a halfway house.

    * Details depend also on whether it's just you or you're collaborating and/or keeping an audit trail. Are others bound to use similar software?
    Last edited by Nick Cox; 09 May 2022, 08:12.

    Comment


    • #3
      This particular code is only for a small internal team, the speed isn't a particular issue and your suggestion works great. Thank you!

      Comment

      Working...
      X