Announcement

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

  • keep the last observation(s) in each year

    Dear All, Suppose that the data set is
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str6 Stkcd str10 riqi byte(Position Changtyp Edca Dimreas Dimage) double Years byte Entele
    "000001" "2002-04-30" 1 1 4  . 56  3.17 .
    "000001" "2003-09-04" 1 2 4  .  .     . 1
    "000001" "2003-10-16" 2 1 4  3 52     6 .
    "000001" "2003-10-16" 2 2 4  .  .     . 1
    "000001" "2004-12-14" 1 1 4  3 53     1 .
    "000001" "2004-12-14" 1 2 5  .  .     . 2
    "000001" "2004-12-14" 2 1 4  3 42     1 .
    "000001" "2004-12-14" 2 2 4  .  .     . 2
    "000001" "2005-05-16" 1 1 5  5 62    .5 .
    "000001" "2005-05-16" 1 2 3  .  .     . 1
    "000001" "2005-06-17" 1 2 3  .  .     . 1
    "000001" "2005-06-17" 1 1 3 12 62    .1 .
    "000002" "2006-02-11" 2 1 4  5 53   1.1 .
    "000002" "2007-02-07" 2 2 2  .  .     . 1
    "000002" "2010-06-29" 1 1 3  5 68     5 .
    "000002" "2010-06-29" 1 2 2  .  .     . 1
    "000002" "2010-06-29" 2 1 2  1 62  3.33 .
    "000002" "2010-06-29" 2 2 .  .  .     . 1
    "000002" "2010-10-13" 2 1 . 12 54   .25 .
    "000002" "2010-10-13" 2 2 .  .  .     . 1
    "000002" "2012-11-21" 2 1 .  8 56  2.08 .
    "000002" "2012-11-21" 2 2 5  .  .     . 2
    "000002" "2012-11-21" 1 1 .  8 64  2.42 .
    "000002" "2012-11-21" 1 2 2  .  .     . 2
    "000003" "2016-10-20" 2 1 5  5 59  3.92 .
    "000003" "2016-10-20" 2 2 4  .  .     . 1
    "000003" "2016-11-06" 1 1 2  3 63  3.96 .
    "000003" "2016-11-07" 1 2 4  .  .     . 1
    "000003" "2016-12-10" 1 1 4 12 45   .09 .
    "000003" "2016-12-10" 1 2 5  .  .     . 2
    "000003" "2016-12-10" 2 1 4 12 54   .14 .
    "000003" "2016-12-10" 2 2 4  .  .     . 1
    "000003" "1999-02-08" 2 1 .  9 48     8 .
    "000003" "1999-02-08" 2 2 3  .  .     . 1
    "000004" "2001-02-15" 2 1 3  7 49     2 .
    "000004" "2001-02-15" 2 2 4  .  .     . 1
    "000004" "2002-06-12" 1 2 3  .  .     . 1
    "000004" "2017-06-29" 1 1 3  3 66 15.06 .
    "000004" "2017-06-30" 1 2 4  .  .     . 1
    end
    For each Stkcd and each year, I'd like to keep the last observation(s) occurred in each year.

    Note that, if there are more than one observation with the same last date, keep them all. Thanks.
    Ho-Chuan (River) Huang
    Stata 19.0, MP(4)

  • #2
    you could do,
    Code:
    gen year = yofd(date(riqi,"YMD"))
    egen group = group(Stkcd year riqi)
    bys Stkcd year: egen last = max(group)
    keep if group == last

    Comment


    • #3
      Another solution as below.

      Code:
      gen date = date(riqi, "YMD")
      gen year = yofd(date)
      bys Stkcd year (date): keep if date == date[_N]

      Comment


      • #4
        Dear Øyvind, Thanks for this helpful suggestion.
        Ho-Chuan (River) Huang
        Stata 19.0, MP(4)

        Comment


        • #5
          Dear Professor Wang, Thanks for this helpful suggestion.
          Ho-Chuan (River) Huang
          Stata 19.0, MP(4)

          Comment

          Working...
          X