Announcement

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

  • "not sorted" error

    Dear all

    I have two grouping variables (var1 var2). I want to sort them (sort var1 var2) and then create a new variable for each group:

    ******************
    clear *
    set obs 100
    gen var1=round(runiform(1,10),1)
    gen var2=round(runiform(1,10),1)

    sort var1 var2
    by var1 var2: gen var3=_n

    ********************

    the above works fine for sure. But if I try to achieve the same results with the following, there is a "not sorted" error, when the data should have been sorted:


    ***********************
    clear *
    set obs 100
    gen var1=round(runiform(1,10),1)
    gen var2=round(runiform(1,10),1)

    sort var1 var2
    gen groupsort=_n

    sort groupsort
    by var1 var2: gen var3=_n // caused "not sorted" error

    ***************************

    The reason for exploring the second approach is to (i hope) help speed up sorting by having to sort only a single variable.

    Grateful for advice.

    Charlie

  • #2
    You've already sorted by var1 var2. so there is no need to sort again. What's more, the sort groupsort not only does not change the sort order of the data, it rules out the by var1 var2.

    You can't possibly speed anything up by doing it twice, even if the second time it's done faster.

    Comment


    • #3
      Originally posted by Nick Cox View Post
      You've already sorted by var1 var2. so there is no need to sort again. What's more, the sort groupsort not only does not change the sort order of the data, it rules out the by var1 var2.

      You can't possibly speed anything up by doing it twice, even if the second time it's done faster.
      Thanks Nick. The reason I tried to do this is because I need to sort var1 var2 for many times in my work (Grateful for suggestions on speeding this up). The curious part to me is why Stata said it's "not sorted" when indeed it is.

      Comment


      • #4
        Nothing curious about it once you understand how Stata works.. The prefix by var1 var2 requires a previous sort var1 var2. The fact that the result of sort groupsort is the same is neither here nor there, because Stata will not go off to check that it is the same. Stata just registers the most recent sort order as the information it uses to check on sort order.

        It is like if you said that your new name is Danny. or whatever. Once you've said that, Charlie is your previous name, regardless of the fact that you're the same person. The answer to questions about your name is now Danny.

        Incidentally, quoting the entirety of a previous answer is neither needed nor particularly useful. The previous answer is visible. The point of quotation is to draw attention to a particular part of the text.
        Last edited by Nick Cox; 04 Nov 2017, 11:03.

        Comment


        • #5
          thanks for the reply Nick.

          Comment

          Working...
          X