Announcement

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

  • Count occurences of levels of a categorical variable

    Hi,

    I am new to Stata.

    I am trying to generate a new variable that counts the occurrence of each level of my categorical variable (4 levels), by id. Each id occurs 15 times (15 rows per id). So far I have found the following solution:

    Code:
    by id: count if Variable_cat==`"A"'
    This does give me an answer but it is formatted as a list. I would like the result to be printed in a new variable/ vector so that I can use it in subsequent analyses. I understand that there will only be one row with a value for each individual for this new variable.

    Thanks for your help!

  • #2
    Code:
    bysort id Variable_cat: gen wanted = _N
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      This was very helpful!
      Thanks Maarten (dank je wel!) for your quick reply!


      Comment

      Working...
      X