Announcement

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

  • Creating 'count' variables

    Hello all! I am attempting to create a variable that counts for a certain category of another variable. For example, there are 4 variables in my data (22a, 22b, 22c, 22f). They both have two categories (0 and 1). How would I create a new code (for ex: countadl) that counts each answer that falls under the '1' category, for all four variables? Ideally, my new count variable would have all of the '1's from all of the 4 variables.

  • #2
    Welcome to Statalist.

    Use the command help egen and read up on anycount.

    Please also take some time to read the FAQ (http://www.statalist.org/forums/help) and understand how using dataex to data in code form. That would be very helpful for users here to test their codes and help you.

    Code:
    clear
    input v1 v2 v3
    1 0 0
    0 0 0
    1 1 1
    end
    
    egen wanted = anycount(v1 v2 v3), values(1)
    list
    Results:

    Code:
         +-----------------------+
         | v1   v2   v3   wanted |
         |-----------------------|
      1. |  1    0    0        1 |
      2. |  0    0    0        0 |
      3. |  1    1    1        3 |
         +-----------------------+
    Last edited by Ken Chui; 23 Feb 2023, 14:38.

    Comment


    • #3
      Thank you!

      Comment

      Working...
      X