Announcement

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

  • keep observations that have the max value of variables for a caseID

    Dear Statalist,

    I'm trying to keep observations that have the max value of a variable for multiple variables for a caseID so that there is only one caseID with the max number for the variable. All of the variables are binary outcomes. For example, in the dataset below, I would like to collapse the CaseIDs so that there is one CaseID for 1 and 2 and keep the highest number for orange, apple, and banana for each CaseID. I'd like to get table 1 to look like table 2. Thanks in advance for any guidance!

    Table 1
    CaseID Orange Apple Banana
    1 1 0 0
    1 0 1 0
    1 0 0 1
    1 0 0 0
    2 0 0 1
    2 0 0 0
    2 0 0 0
    Table 2
    CaseID Orange Apple Banana
    1 1 1 1
    2 0 0 1

  • #2
    Code:
    collapse (max) Orange Apple Banana, by(CaseID)
    Do read -help collapse-. It is one of the "bread and butter" data management commands in Stata.

    Comment


    • #3
      thank you!

      Comment

      Working...
      X