Announcement

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

  • Restore orginal order variables

    Dear Statalisters,

    As I am working on a quite large database, I am changing the names of the variables and also their order often because I need to clean the file. But once I'm done with cleaning, I would like to restore the original order of the database.

    I can click on all the variables to order them by hand, but as there are a lot of variables this is time-consuming. I read the files about 'order', but I could not find a command which can restore the order to the basic lay out when opening the document. Does any of you guys have a good alternative?

    Thanks in advance,
    Mariska

  • #2
    A basic approach could be

    Code:
    /*
        put all variables in their 
        original order in local -allnames-
    */
    unab allnames : *
    
    ...here goes your code
    
    /*
        restore the original order 
    */
    order `all'
    Since you are changing variable names and might even drop some variables or create new ones, you need to update the name in local allnames. For example

    Code:
    rename foo bar
    local allnames : subinstr local allnames " foo " " bar "
    Best
    Daniel

    Comment


    • #3
      You can restore the original order easily if you save it at the outset.

      Code:
       
      unab original : *
      should put the variable names in a local macro. After a lot of cleaning

      Code:
       
      order `original'
      should put back the original order. That should work even if you have created new variables; any such just being put after the original variables.

      You could also do some variant on this even if you never put those variable names in a macro:

      Code:
       
      save current 
      use original 
      unab original : * 
      use current 
      order `original' 
      save original, replace
      With a really big dataset that's a lot of heaving around. It's also programmable in principle just to read in the names by looking at the original .dta, but I could not write down the code for you; that may have been done already.

      See also reorder from SSC. I've not used or even looked at that since I wrote it, and it doesn't do directly what you ask, but it's in similar terrritory.

      Comment


      • #4
        Dear Daniel and Nick,

        Thank you for your advice, it's an excellent code; happy with it !

        Best,
        Mariska

        Comment

        Working...
        X