Announcement

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

  • Equivalent to "inlist" for AND conditions?

    I'm familiar with inlist, and generally use it in the format:

    "gen example=1 if inlist(1,var1,var2,var3)"

    as equivalent to "gen example=1 if 1==var1|1==var2|1==var3"

    My question: Is there an equivalent to inlist for & conditions?

    i.e. Ideally something to the effect of:

    "gen example2=1 if function(1,var1,var2,var3)"

    as equivalent to "gen example2=1 if 1==var1&1==var2&1==var3"


    Thanks,

    James

  • #2
    I don't know of any single function that would do this. But the following is fairly concise:

    Code:
    egen example2 = anycount(var1 var2 var3), values(1)
    replace example2 = (example2 == 3)
    Added: In practice, you probably should not follow the coding style used in your original post. Your variable example will be coded as 1 for true and missing value for false. But nothing else in Stata is set up to work with that coding. Boolean variables are most effectively coded as 0/1, not ./1. So, a better version of your initial example would be:
    Code:
    gen example = inlist(1, var1, var2, var3)
    which gives a 0/1 result.
    Last edited by Clyde Schechter; 27 Sep 2016, 12:16.

    Comment


    • #3
      This is great, thanks for the tips Clyde!

      Comment

      Working...
      X