Announcement

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

  • Not sorted when conducting bysort egen command even after xtset and sort

    I found this posting useful:
    https://www.statalist.org/forums/for...rage-by-groups
    My bad that I didn't check the previous questions

    (may discard below)
    /*------------------------------------------------------------------------------------------------------------------
    Hello,

    I got into a weird and simple trouble when I tried to generate a three-year moving average.
    My commands are followed ("id" is the unique identifier and "year" is the time variable):

    Code:
    xtset id year
    [output]
           panel variable:  id (unbalanced)
            time variable:  year, 1979 to 2015, but with gaps
                    delta:  1 unit
    
    sort id year
    bysort id year: egen average= mean(L.k1 + L2.k1 + L3.k1)
    And I get this error: "not sorted"
    I don't have duplicates when I do "duplicates list id year".

    What could be potential issues here??
    I have 514 id's.

    By the way, I am using State 14.0 in Windows 10.
    Last edited by Minjae Yun; 02 Dec 2019, 13:45.

  • #2
    bysort id year is redundant here as id year pairs define observations uniquely. If you want the mean of the previous three values then that is, after xtset,

    Code:
    gen wanted = (L1.k1 + L2.k1 + L3.k1)/3
    In contrast, the mean of a constant sum is just that sum. The mean of (1 + 2 + 3) is 6, not 2.

    All that said, tssmooth ma returns moving averages. Writing your own code can be instructive, but an official command already exists.
    Last edited by Nick Cox; 03 Dec 2019, 02:06.

    Comment

    Working...
    X