Announcement

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

  • Question about Count if function

    New STATA user here. I have a large dataset and I can't figure out how to use count if function if I want to use both the and and "or" operator. For example, I want to find how many cells in the race category have 6 (multi racial) AND which of those has a "Yes" (ie 1) value in the Native columns or the Hawaiian columns. I am able to get the first part

    count if RaceEthn ==6 & hawaiipi ==1

    How do I add the "OR" part of the command, ie amiakn ==1

    Thank you.
    Last edited by RT Cee; 21 Feb 2023, 13:50.

  • #2
    Welcome to Statalist.

    See
    Code:
    help operators
    which shows you Stata's operators. Note that the & takes precedence over the| , hence you have to choose round brackets accordingly. See also the .pdf Stata manual "[U] 13.2 Operators" accessible via -help operators-.

    Comment


    • #3
      Thank you! If I understand correctly, then it would be:
      count if RaceEthn ==6 & (hawaiipi==1 | amiakn==1)

      and that would count total without counting duplicates. ie a value of 6 in RaceEthn with 1 in both hawaiipi and amiakn would be counted once, not twice, correct?

      Comment


      • #4
        -count- will count the number of cases for which it is true that RaceEthn is equal to 6 and at the same time (either hawaiipi is equal to 1 or amiakn is equal to 1).

        To see this more clearly, have a look at the following commands and before running them first try to anticipate which of the 23 cases will be listed (and the number of valid cases for each variable) if the condition of the last of the four -list- commands holds:
        Code:
        clear
        input case ethn v1 v2
         1  1 0 0
         2  2 0 1
         3  3 1 0
         4  4 1 1
         5  5 . .
         6  6 0 0
         7  6 0 1
         8  6 0 .
         9  6 1 0
        10  6 1 1
        11  6 1 .
        12  6 . 0
        13  6 . 1
        14  6 . .
        15  . 0 0
        16  . 0 1
        17  . 0 .
        18  . 1 0
        19  . 1 1
        20  . 1 .
        21  . . 0
        22  . . 1
        23  . . .
        end
        
        list ethn v1 v2, sepby(ethn) N
        list ethn v1 v2 if ethn==6, sepby(ethn) N
        list ethn v1 v2 if ethn==6 & v1==1 | v2==1, sepby(ethn) N
        list ethn v1 v2 if ethn==6 & (v1==1 | v2==1), sepby(ethn) N
        Then have look at the result and see whether your anticipation was correct:
        Code:
        . list ethn v1 v2, sepby(ethn) N
        
              +----------------+
              | ethn   v1   v2 |
              |----------------|
           1. |    1    0    0 |
              |----------------|
           2. |    2    0    1 |
              |----------------|
           3. |    3    1    0 |
              |----------------|
           4. |    4    1    1 |
              |----------------|
           5. |    5    .    . |
              |----------------|
           6. |    6    0    0 |
           7. |    6    0    1 |
           8. |    6    0    . |
           9. |    6    1    0 |
          10. |    6    1    1 |
          11. |    6    1    . |
          12. |    6    .    0 |
          13. |    6    .    1 |
          14. |    6    .    . |
              |----------------|
          15. |    .    0    0 |
          16. |    .    0    1 |
          17. |    .    0    . |
          18. |    .    1    0 |
          19. |    .    1    1 |
          20. |    .    1    . |
          21. |    .    .    0 |
          22. |    .    .    1 |
          23. |    .    .    . |
              |----------------|
            N |   14   16   16 |
              +----------------+
        
        . list ethn v1 v2 if ethn==6, sepby(ethn) N
        
              +----------------+
              | ethn   v1   v2 |
              |----------------|
           6. |    6    0    0 |
           7. |    6    0    1 |
           8. |    6    0    . |
           9. |    6    1    0 |
          10. |    6    1    1 |
          11. |    6    1    . |
          12. |    6    .    0 |
          13. |    6    .    1 |
          14. |    6    .    . |
              |----------------|
            N |    9    6    6 |
              +----------------+
        
        . list ethn v1 v2 if ethn==6 & v1==1 | v2==1, sepby(ethn) N
        
              +----------------+
              | ethn   v1   v2 |
              |----------------|
           2. |    2    0    1 |
              |----------------|
           4. |    4    1    1 |
              |----------------|
           7. |    6    0    1 |
           9. |    6    1    0 |
          10. |    6    1    1 |
          11. |    6    1    . |
          13. |    6    .    1 |
              |----------------|
          16. |    .    0    1 |
          19. |    .    1    1 |
          22. |    .    .    1 |
              |----------------|
            N |    7    8    9 |
              +----------------+
        
        . list ethn v1 v2 if ethn==6 & (v1==1 | v2==1), sepby(ethn) N
        
              +----------------+
              | ethn   v1   v2 |
              |----------------|
           7. |    6    0    1 |
           9. |    6    1    0 |
          10. |    6    1    1 |
          11. |    6    1    . |
          13. |    6    .    1 |
              |----------------|
            N |    5    4    4 |
              +----------------+

        Comment

        Working...
        X