Announcement

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

  • Generating count variable for observations per year

    I have the following (simplified) dataset

    Person_ID Fiscal_Year
    101 2015
    101 2015
    101 2016
    102 2016
    102 2017
    102 2017
    103 2015
    103 2016
    103 2017
    I would like to generate a variable that counts the Person_ID's per Fiscal year.
    So, for example, the variable should contain the value 2 because the person ID is two times in the Fiscal_Year 2015.

    The values of the new variable should be as follows:
    Person_ID Fiscal_Year New_Variable
    101 2015 2
    101 2015 2
    101 2016 1
    102 2016 1
    102 2017 2
    102 2017 2
    103 2015 1
    103 2016 1
    103, 2017 1

    Could anyone help me out?

    Thnx in advance!

  • #2
    Joe:
    Code:
    . bysort Person_ID Fiscal_Year: egen counter=count( Person_ID )
    
    . list
    
         +-------------------------------+
         | Person~D   Fiscal~r   counter |
         |-------------------------------|
      1. |      101       2015         2 |
      2. |      101       2015         2 |
      3. |      101       2016         1 |
      4. |      102       2016         1 |
      5. |      102       2017         2 |
         |-------------------------------|
      6. |      102       2017         2 |
      7. |      103       2015         1 |
      8. |      103       2016         1 |
      9. |      103       2017         1 |
         +-------------------------------+
    
    .
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Or just

      Code:
       
       bysort Person_ID Fiscal_Year: gen counter = _N

      Comment


      • #4
        Thnx a lot both of you!

        Comment

        Working...
        X