Announcement

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

  • Use of gsort command with by

    Dear Statalist users,
    I want use this command to sort data in descending order, then generate new variable as shown below.
    gsort Vdsid_hhid Year NameoftheSeason Ownershipstatus Codeoftheplot Nameoftheplot -Percentageareaofthecrop
    by Vdsid_hhid Year NameoftheSeason Nameoftheplot Codeoftheplot Plotcropareainacres Ownershipstatus: gen crop=_n

    But error shows that - "Not sorted"
    but when i use bysort instead of by, descending order disturb.
    Please help in resolving the issue.
    Thanks

  • #2
    The problem is that the variables you are specifying in the -by- prefix do not correspond to the variables in the -gsort- command. In the -by- command you have moved Nameoftheplot in front of Codeoftheplot, which it appears after in the -gsort- command. You have also changed the location of Ownershipstatus, and added a new variable Plotcropareainacres. So the sort order you are specifying in the -by- prefix is very different from the one you have obtained from -gsort-, which is why Stata complains.

    Comment


    • #3
      Thanks Clyde Schechter, It worked

      Comment


      • #4
        Also note that Stata does not recognize descending sort order, which gsort implements. This is a small request of mine for 2 versions now.

        If you need descending sorted values, you must construct your own variable to do this.

        Comment


        • #5
          If you need descending sorted values, you must construct your own variable to do this.
          Well, not necessarily. If the descending sorted variables will be part of the definition of the by-group, it usually not important whether it be sorted ascending or descending. That typically only matters on those variables that are not group-defining and must be sorted in order for _n and _N to work properly--the ones that are put in parentheses in the -by:- prefix. The situation where a variable is part of the by-group definition and its sort order (ascending vs descending) also matters is, I think, uncommon. I cannot recall encountering such a situation in my workflow, although I can think of some relatively artificial examples where this would happen.

          And for those variables that find themselves in parentheses, you can just use -gsort- before the -by- command, and then in the -by- command mention only the non-parenthesized variables, without using -, sort- or -bysort- and all will be well.

          Comment


          • #6
            That’s true, Clyde. Most of the time it’s not really necessary to need the descending sort, though I have run into the odd situation where it would have been helpful. By the same token, there really isn’t any logical reason why Stata should insist on only accepting the ascending order for “official” sorting, by which I mean what is internally recognized as the sort key.
            Last edited by Leonardo Guizzetti; 26 Aug 2022, 18:44.

            Comment


            • #7
              By the same token, there really isn’t any logical reason why Stata should insist on only accepting the ascending order for “official” sorting, by which I mean what is internally recognized as the sort key.
              Yes, I agree. I've never understood why the functionality of -gsort- hasn't been built into -sort- and -by, sort-. Maybe there's a good reason I don't see, but it's hard for me to imagine what it might be.

              Comment


              • #8
                Thanks for useful discussion

                Comment

                Working...
                X