Announcement

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

  • Rename nonconsecutive to consecutive (and back again!)

    I have the following data (first time using dataex, apologies if I did it right)

    input long id byte(v1271 v1272 v12113 v12137)
    12530 . . . .
    71538 . . . .
    168244 . . . .
    172310 . . . .
    172774 . . . .
    173127 . . . .

    I would like to rename v* so that the numbers are consecutive, ie, v1271 to v1, v1272 to v2, etc. I have thousands of variables, so listing the new variable names is not an option. I have played with just about every option associated with "rename" I know and nothing's working.

    Added fun twist: at some point in post estimation, I will need to restore the original variable names. So pre-estimation, v1271 needs to become v1, but post-estimation, I need to reset the name to v1271.

    Thanks in advance,

    Bryan

  • #2
    Well, if you didn't need to restore the original names later, I think all you would need is:

    Code:
    rename v# v#, renumber
    But given that you need to eventually restore the original names, you have to make a record of them along the way, and -rename- will not do that for you. So something like this:

    Code:
    local i = 1
    foreach v of varlist v* {
        char `v'[original_name] "`v'"
        rename `v' vvv`i'
        local ++i
    }
    rename vvv* v*
    Note: The reason for renaming to vvv`i' instead of v`i' is to avoid a possible name clash. That is, when you rename v123, as the 48th such variable, while you want to call it v48, there might be an original variable v48 hanging around and not yet renamed (since your originals are not in order). So temporarily we use a different stub, and the bulk rename them at the end.

    When it comes time to go back to the original names it's just the same process in reverse:

    Code:
    rename v* vvv*
    foreach v of varlist vvv* {
        local original_name: char `v'[original_name]
        rename `v' `original_name'
    }

    Comment


    • #3
      Can I suggest that, instead of renaming v1271 to v1, etc., you rename v1271 to u1, or to just about anything other than something starting with v? That way it will be immediately clear at all times, including 5 months after you thought the task was done when you are forced to revise your work to address reviewer criticisms, that a reference to v1271 is to the variable with v1271 as its original name, and a reference to u1271 is to whichever variable was renamed to u1271. Seeing "v1271" in code or output risks lack of clarity and confusion, especially after some time away from the project (in my case, on Monday after a weekend off).

      Note that I, like Clyde, am taking as a given your desire to rename the variables en masse. I would not do that myself; I cannot imagine what benefit would arise that couldn't be obtained relatively easily in Stata some other way. I hope, for example, it isn't because you are not familiar with all the options for variable lists presented by help varlist.

      Comment


      • #4
        rename optionally returns the old to new name mappings.

        Comment


        • #5
          Clyde, your solution got me 95 percent of the way. Used consecutively

          Code:
          foreach v of varlist v* {
              char `v'[original_name] "`v'"
              rename `v' vvv`i'
              local ++i
          }
          and

          Code:
          rename v* vvv*
          foreach v of varlist vvv* {
              local original_name: char `v'[original_name]
              rename `v' `original_name'
          }
          toggle back and forth between sets of variable names nicely.

          However, what I've tried to do is rename to the v1 v2... set, run my model, change back to the old set, and output the results in terms of the old set of names. The code I'm using:

          Code:
          local i = 1
          foreach v of varlist v* {
              char `v'[original_name] "`v'"
              rename `v' vvv`i'
              local ++i
          }
          rename vvv* v*
          
          use v1-v100 using "C:\xxxx.dta", clear
          quietly irt 2pl v1-v100
          
          rename v* vvv*
          foreach v of varlist vvv* {
              local original_name: char `v'[original_name]
              rename `v' `original_name'
          }
          estat report
          I would like estat to show model results in terms of the old variable names (v1271, v1272, v12113, etc.), but instead it outputs in terms of v1 v2... Any way to change that?

          To everyone else in post, I welcome another solution, but I've tried everything I know and some other tips I've picked up here to no avail over the past few days. I will need to group together a large number (over 2800) of IVs into groups of ~100, run
          Code:
          irt 2pl
          , and output the estimation results to Excel. I will then need to do that for approximately 40 more similar data sets with a similarly ridiculously large number of IVs, so I need code that I can use again with little to no modification. Yes, the data is wide and needs to be, given the requirements of irt 2pl.

          Comment


          • #6
            I would like estat to show model results in terms of the old variable names (v1271, v1272, v12113, etc.), but instead it outputs in terms of v1 v2... Any way to change that?
            I cannot think of any reasonable way to accomplish that.

            I don't understand why you are using this approach. What is the point of renaming the variables? It seems like you are just looking for a way to process the variables in batches of 100. If that is the case, there is no need to rename them. Just use them as they are in the order they are in batches of 100:

            Code:
            quietly ds v*
            local vlist `r(varlist)'
            tokenize vlist
            while "`1'" != "" { // ITERATE OVER LISTS OF 100
                local list100 // START A LIST OF 100
                forvalues j = 1/100 { // BUILD THE LIST OF 100 VARIABLES
                    local list100 `list100' `1'
                    macro shift
                }
                irt 2pl `list100' // USE THE LIST OF VARIABLES
                estat report
            } // MOVE ON TO NEXT LIST OF 100

            Comment


            • #7
              daniel klein I did not know that -rename- could return the lists of old and new names. Thank you for pointing that out. That could come in handy!

              Comment


              • #8
                Perhaps this technique will avoid all the renaming.
                Code:
                // set up 2050 variable names for testing
                clear
                set obs 1
                forvalues i=1/2050 {
                generate byte v`= 1000 + 3*`i'' = 1
                }
                // get a list of variable names
                quietly ds v*
                local vars `r(varlist)'
                local nvar : word count `vars'
                // loop through them 100 at a time
                forvalues i=1(100)`nvar' {
                    local chunk
                    local j = min(`i'+99,`nvar')
                    local first : word `i' of `vars'
                    local last  : word `j' of `vars'
                    unab chunk : `first'-`last'
                    display _newline "irt 2pl `chunk' "
                    }
                As an exercise for the reader, confirm that the following syntax would work, avoiding the unab command and explicit list of 100 variables.
                Code:
                ird 2pl `first'-`last'
                Last edited by William Lisowski; 17 Jul 2016, 20:09.

                Comment

                Working...
                X