Announcement

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

  • Rename help: can I reapply existing variable names with a suffix?

    I've been given some wide panel data where the names of the first 28 variables were simply repeated over and over in sequence without any suffix. Stata wouldn't allow this during the import, so all subsequent variables after the first 28 simply have the column letters from excel. Is there a way I can tell Stata to reuse the initial set of variable names across the next 28 variables (or ideally, all 3 sets of 28) while applying a suffix to make them unique?

    Thanks,
    Joe

  • #2
    Yes, this can be done. To illustrate the process, I have used a fragment of the auto.dta and modified its variable names to resemble your situation. The only substantive difference from what you describe is that this example works in groups of 4, not 28. But if you just change 4 to 28 at the indicated place in the code below, this should do what you ask for:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str18 make int(price mpg rep78) float E int(F G H I J) float K byte L
    "AMC Concord"   4099 22 3 2.5 11 2930 186 40 121 3.58 0
    "AMC Pacer"     4749 17 3   3 11 3350 173 40 258 2.53 0
    "AMC Spirit"    3799 22 .   3 12 2640 168 35 121 3.08 0
    "Buick Century" 4816 20 3 4.5 16 3250 196 40 196 2.93 0
    "Buick Electra" 7827 15 4   4 20 4080 222 43 350 2.41 0
    end
    label values L origin
    label def origin 0 "Domestic", modify
    
    local clump_size 4  // MAKE THIS 28 FOR YOUR APPLICATION
    
    local clump_end `clump_size'
    
    
    quietly ds
    local all_vars `r(varlist)'
    local n_vars: word count `all_vars'
    local suffix 1
    macro dir
    
    while `clump_end' < `n_vars' {
        forvalues i = 1/`clump_size' {
            rename `:word `=`clump_end' + `i'' of `all_vars'' ///
                `:word `i' of `all_vars''`suffix'
        }
        local ++suffix
        local clump_end = `clump_end' + `clump_size'
    }

    Comment


    • #3
      I apologize for such a belated thank you, but thank you!

      Comment

      Working...
      X