Announcement

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

  • Order dataset alphabetically by variable label

    Hello,

    How can order my dataset alphabetically by variable label?

    Thanks!
    Reese

  • #2
    Assuming, as the FAQ says we can assume in the absence of a declaration to the contrary, that you are running version 16, this can be done as follows:

    Code:
    clear*
    sysuse auto, clear
    
    frame create ordering str32 varname strL varlabel
    
    foreach v of varlist _all {
     frame post ordering (`"`v'"') (`"`:var label `v''"')
    }
    
    frame change ordering
    sort varlabel varname
    
    gen order = ""
    replace order = order[_n-1] + " " + varname
    local desired = order[_N]
    
    frame change default
    order `desired'
    If you are running an older version of Stata that does not support frames, you can do something similar using a -postfile- instead of a frame to do the ordering.

    Comment


    • #3
      Hi Clyde,

      Thanks for your help. I apologize for the mistake, but I forgot to specify that I'm using Stata version 14. How would I modify your code for the -postfile- method? Both -frame- and -postfile- are completely new to me.

      Again, apologies for not being clear about my version in the original post.

      Thanks,
      Reese

      Comment


      • #4
        Code:
        clear*
        sysuse auto, clear
        
        tempfile ordering
        postfile handle str32 varname str244 varlabel using `ordering'
        
        foreach v of varlist _all {
         post handle (`"`v'"') (`"`:var label `v''"')
        }
        postclose handle
        
        preserve
        use `ordering', clear
        sort varlabel varname
        
        gen order = ""
        replace order = order[_n-1] + " " + varname
        local desired = order[_N]
        
        restore
        order `desired'
        Changes from the earlier code are shown in boldface.

        There is one slight difference in functionality between this version and the original. -postfile-s do not support -strL-s, so in this version, only the first 244 characters of the variable label can be dealt with. As a practical matter, I doubt this will ever bite--even if your data set has variable labels longer than that, it seems unlikely that it would have variable labels that are different but agree on all of their first 244 characters. But if that is the case, then this code could fail to order them correctly.

        Comment


        • #5
          You're amazing. Super helpful as always. Thank you Clyde!

          Comment


          • #6
            Hi,

            I wonder how can this be done, that is, order my dataset alphabetically, but by variable name?

            Thanks!

            Comment


            • #7
              Code:
              order _all, alphabetic

              Comment


              • #8
                Code:
                ds, alpha
                sorts the names in order but doesn't change the dataset order.

                Comment


                • #9
                  Awesome. Thanks Clyde!

                  Comment


                  • #10
                    Hello everyone,

                    by chance, has there been a change up to STATA 18.0 allowing for a less complicated ordering of a dataset by varlabels? I am aware of Clyde Schecters solution, but would love an easier way for the sake of an introductory seminar.
                    Thanks!

                    Comment


                    • #11
                      No such updates as this is not a common use case. It is more typical that variables are ordered by name, since that can be useful for programming convenience. I’ve never wanted/needed to sort anything by variable label, but you might have a good reason for it.

                      Comment


                      • #12
                        I think that quite a good point for an introductory seminar would be threefold:

                        1. This isn't often asked or (it seems) often needed.

                        2. But it is programmable!

                        3. So, if you want it, the code written by an experienced user can be wrapped up in a new command.

                        And this is how the user community can extend Stata -- and all users benefit to differing degrees from extensibility.

                        Comment

                        Working...
                        X