Announcement

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

  • egen ans count()

    I have a list of patients with 1-6 visits each. At each visit, a score is registered (integer from 1-6).

    I wish to count for each patient the number of times they obtain at particular score, e.g. of 3.

    And tabulate how many patients obtain a score of 3 once, twice, etc.

    I have tried this:

    bysort PatientID: egen numberOfScoresEqualTo3=count(Score==3)

    But STATA keeps counting all the visits, no matter what the score is. E.g. a patient with 4 visits will be counted as 4 despite having obtained a score of 3 only twice.

    What is wrong? It seems such a simple problem!

    Thanks for any help or alternative suggestions!

  • #2
    The correct code for your problem is:
    Code:
    bysort PatientID: egen numberOfScoresEqualTo3=total(Score==3)
    You misunderstood what the -egen, count()- function does. -count(expression)- returns the number ofobservations in which the value of expression is not missing. Now, an expression like Score == 3 is never missing, since Score == 3 is always either true (1) or false(0). So count(Score == 3) will just be the number of observations (visits).

    Comment


    • #3
      Ah, but of course.. all true expressions return a numerical value of 1, and these may be added by total. Simple and elegant.

      Thank you for your swift reply and this useful insight!

      And for your help - your solution works great!

      Have a nice day
      Hans

      Comment

      Working...
      X