Announcement

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

  • sort versus gsort

    1. Why do we have both -sort- and -gsort-?
    2. Why isn't there a corresponding bygsort prefix, so you can do things like -bygsort panelvar (-time): gen event_in_rev_chron_order =_n-?
    Are there historical or computational reasons?

  • #2
    I don't have an answer to the larger question but for your specific example you can get what you want without reverse sorting.

    Code:
    bysort panelvar (time): gen event_in_rev_chron_order=_N-_n+1

    Comment


    • #3
      I don't have an answer to the second question, but on the first one:
      sort is the sorting engine - internal, performance optimized command to sort observations in ascending order;
      gsort is a convenience wrapper - implemented as an ado file, hardly changed since version 6, and allowing among other things sorting in descending order. For that it creates temporary variables, which (I expect) make it slower.

      Best, Sergiy Radyakin

      Comment


      • #4
        gsort doesn't really fit well with anything else in Stata. It is in some ways more of a specialised sort command than a generalised one! I'd guess it will remain that way.

        Another specific answer to Dimitriy's question is

        Code:
         
        bysort panelvar: egen event_in_rev_chron_order = rank(-time)

        Comment


        • #5
          These are all very helpful responses.

          Comment

          Working...
          X