Announcement

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

  • if command with 3 conditions

    I am trying to generate a new variable using the "if" command based on 3 other variables
    ex:
    gen completetx=1 if (neo==1 & surgery==1 & adjuvant==1)

    However, it seems that stata only reads the first 2 conditions, and ignores the third. Am I doing something wrong? Is it possible to have more than 2 conditions with an if command? Thanks!

  • #2
    Originally posted by Amaranta Losey View Post
    it seems that stata only reads the first 2 conditions, and ignores the third.
    I can't replicate your problem.

    .ÿversionÿ16.1

    .ÿclearÿ*

    .ÿquietlyÿsetÿobsÿ6

    .ÿgenerateÿbyteÿneoÿ=ÿ_nÿ>=ÿ_Nÿ/ÿ2

    .ÿgenerateÿbyteÿsurgeryÿ=ÿmod(_n,ÿ2)

    .ÿsortÿsurgeryÿneo

    .ÿgenerateÿbyteÿadjuvantÿ=ÿmod(_n,ÿ2)

    .ÿgenerateÿbyteÿcompletetxÿ=ÿ1ÿifÿ(neo==1ÿ&ÿsurgery==1ÿ&ÿadjuvant==1)
    (5ÿmissingÿvaluesÿgenerated)

    .ÿlistÿifÿneoÿ&ÿsurgery,ÿnoobsÿabbreviate(20)

    ÿÿ+---------------------------------------+
    ÿÿ|ÿneoÿÿÿsurgeryÿÿÿadjuvantÿÿÿcompletetxÿ|
    ÿÿ|---------------------------------------|
    ÿÿ|ÿÿÿ1ÿÿÿÿÿÿÿÿÿ1ÿÿÿÿÿÿÿÿÿÿ1ÿÿÿÿÿÿÿÿÿÿÿÿ1ÿ|
    ÿÿ|ÿÿÿ1ÿÿÿÿÿÿÿÿÿ1ÿÿÿÿÿÿÿÿÿÿ0ÿÿÿÿÿÿÿÿÿÿÿÿ.ÿ|
    ÿÿ+---------------------------------------+


    You'll need to show more in order for anyone to help. See the forum's FAQ for details on how.

    Comment


    • #3
      I agree with Joseph Coveney. Your syntax looks fine. If the three variables are all (0, 1) then a programmer's trick like this is a little tempting

      Code:
      if neo & adjuvant & surgery
      but it is blown by missing values. So, your syntax looks fine.

      Comment


      • #4
        If three variables can each take values 1, 0 (or something else other than 1), or missing, then the 27 possible combinations and the result of applying the generate command are as follows.
        Code:
        . generate vall=1 if (v1==1 & v2==1 & v3==1)
        (26 missing values generated)
        
        . list, noobs sepby(v1 v2)
        
          +---------------------+
          | v1   v2   v3   vall |
          |---------------------|
          |  0    0    0      . |
          |  0    0    1      . |
          |  0    0    .      . |
          |---------------------|
          |  0    1    0      . |
          |  0    1    1      . |
          |  0    1    .      . |
          |---------------------|
          |  0    .    0      . |
          |  0    .    1      . |
          |  0    .    .      . |
          |---------------------|
          |  1    0    0      . |
          |  1    0    1      . |
          |  1    0    .      . |
          |---------------------|
          |  1    1    0      . |
          |  1    1    1      1 |
          |  1    1    .      . |
          |---------------------|
          |  1    .    0      . |
          |  1    .    1      . |
          |  1    .    .      . |
          |---------------------|
          |  .    0    0      . |
          |  .    0    1      . |
          |  .    0    .      . |
          |---------------------|
          |  .    1    0      . |
          |  .    1    1      . |
          |  .    1    .      . |
          |---------------------|
          |  .    .    0      . |
          |  .    .    1      . |
          |  .    .    .      . |
          +---------------------+

        Comment

        Working...
        X