Announcement

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

  • Rename entire list of variables

    I have a .dta file with 390 variables of widely varying names containing both letters and numbers etc and in no patterns or order. I want to change the variable names to the generic v1, v2, v3, ... in the order in which they appear in the variable list (in the dta file). How to I do this?

  • #2
    Try something like the following.
    Code:
    local i 1
    foreach var of varlist _all {
        rename `var' v`i'
        local ++i
    }

    Comment


    • #3
      Very nice!

      Comment


      • #4
        Loops are essential programming tools, and a good thing to practice. But as of Stata 12 rename does no longer need to be used in loops for tasks like this. Try

        Code:
        rename (*) (v#) , addnumber
        Best
        Daniel

        Comment


        • #5
          All that said, I would say that names v1 up would create as many problems as they solve. There is a happy middle ground between anarchy and uniformity. Usually in datasets with that many variables there are blocks of variables with related meaning. It's a real feature of Stata that evocative variable names are possible. I still remain a program whose rules were that all column names began with C.
          Last edited by Nick Cox; 22 Dec 2015, 02:59.

          Comment

          Working...
          X