Announcement

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

  • probelm with using generate by after sorting in descending order

    Dear Stata Users,

    I run the following 2 commands:

    gsort var1 var2 -var3
    by var1 var2 -var3: gen n=_n

    Now stata tells me that the data are not sorted so I can't generate _n (I guess it doesn't get the minus of the gsort command before).
    My question is now wheter there still is a possibility to generate _n with var3 beeing sorted in descending order? (so var3 descending while n ascending)

    Thank you!

  • #2
    gsort does not tell Stat that data are sorted, so you must, for example:
    Code:
    gen var_3 = -var3
    bysort var1 var2 var_3: gen n=_n

    Comment


    • #3
      gsort does not tell Stat that data are sorted, so you must, for example:
      Code:
      gen var_3 = -var3
      bysort var1 var2 var_3: gen n=_n
      or you could:
      Code:
      bysort var1 var2 var3: gen n=_n
      gsort var1 var2 -var3

      Comment


      • #4
        Thank you for your answer, exactly what I was looking for!

        Comment

        Working...
        X