Announcement

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

  • Count if

    Hello all,

    I have a very large house hold survey data set which contains data about the ubication code of the household member' place of birth, I would like to count the foreign people, so I intend to code something like count if "var"!= xxxx | xxxx | xxxx .... but the national codes are more than 2000, so my question is how can I do this using a file text as a list, if possible.

  • #2
    You can merge this list with the variable and then create an indicator for foreigner (=1 if _merge==3). Then use the usual egen functions to count.

    Code:
    help merge

    Comment


    • #3
      It is essential to note that even something like

      Code:
      if foo == 1 | 2 | 3
      is not at all what people often hope it is. It is not an abbreviation for

      Code:
      if foo == 1 | foo == 2 | foo == 3
      It is in essence

      Code:
      if (foo == 1) | 2 | 3
      which contains a logical expression that is always true because (in this example) 2 and 3 are true regardless of foo being 1 or not. 2 and 3 are true because Stata's rule is that non-zero is true. See the longstanding FAQ https://www.stata.com/support/faqs/d...rue-and-false/ (from 2005!)

      Even using " " correctly in contrast something like this

      Code:
      if strvar == "frog" | "toad" | "newt"
      is not even legal, as "toad" and "newt" are pure strings that can't be evaluated for truth or falsity.

      Once again, this syntax is NOT accepted as an abbreviation of

      Code:
      if strvar == "frog" | strvar == "toad" | strvar == "newt"
      which can be written more cleanly as

      Code:
      if inlist(strvar, "frog", "toad", "newt")
      I mention this because #1 seems to be flirting with such syntax (although very likely the quotation marks are in the wrong place too).

      All that said, Andrew Musau has the main point nicely. merge is your friend. This is also a longstanding FAQ https://www.stata.com/support/faqs/d...s-for-subsets/ (from 2011!)




      Comment

      Working...
      X