Announcement

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

  • count observations by two groups

    Hello,

    I would like to count a specific value in the column by two groups.

    This is a part of my dataset.
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte id int year byte moving str2 state
    1 2003 2 "TN"
    1 2004 2 "TN"
    1 2005 5 "TN"
    2 2003 2 "GA"
    2 2004 5 "GA"
    2 2005 2 "GA"
    3 2003 2 "FL"
    3 2004 2 "FL"
    3 2005 4 "FL"
    3 2006 2 "FL"
    4 2003 2 "TN"
    4 2004 2 "GA"
    4 2005 5 "FL"
    5 2003 2 "TN"
    5 2004 2 "FL"
    5 2005 4 "GA"
    5 2006 5 "GA"
    end
    I want to count only "2" in the moving column with the same year and state.
    This is what I want to create a new dataset.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int year byte moving_total str2 state
    2003 3 "TN"
    2003 1 "GA"
    2003 1 "FL"
    2004 1 "TN"
    2004 1 "GA"
    2004 2 "FL"
    2005 0 "TN"
    2005 1 "GA"
    2005 0 "FL"
    2006 0 "GA"
    2006 1 "FL"
    end
    Could you tell me how to create the new dataset from the original dataset.
    Thanks!

  • #2
    Code:
    by year state, sort: egen moving_total = total(moving == 2)
    by year state: keep if _n == 1
    drop id moving

    Comment


    • #3
      Thank you so much, Clyde!

      Comment

      Working...
      X