Announcement

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

  • Create variable 0/1 based on other variables - error message

    Hi I want to create a new variable where the new variable has the value 1 if three other variables have the values 2 in either one of the three variables. In the new variable the value will be 0 if none of the three variables have a value of 2. The new variable will have a missing value if all of the variables has missing values.

    I have tried the following code but got an error message:

    Code:
    generate var1=1 if smoking_a =2 & smoking_a !=. & smoking_b =2 & smoking_b !=. & smoking_c =2 & smoking_c !=.
    invalid syntax
    r(198);
    Last edited by Joe Tuckles; 22 Oct 2019, 04:50.

  • #2
    Joe:
    what it my eyes is the missing double = sign after -if-:
    Code:
     
     generate var1=1 if smoking_a==2 & smoking_a !=. & smoking_b==2 & smoking_b !=. & smoking_c==2 & smoking_c !=
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Hmm.. it seems every value is missing
      Last edited by Joe Tuckles; 22 Oct 2019, 05:07.

      Comment


      • #4
        Joe:
        as a second thought, you can also go -egen-:
        Code:
        . set obs 3
        number of observations (_N) was 0, now 3
        
        . gen A=2
        
        . g B=2 in 2
        (2 missing values generated)
        
        . replace B=1 if missing(B)
        (2 real changes made)
        
        . g C=2 in 1/2
        (1 missing value generated)
        
        . replace C=1 if missing(C)
        (1 real change made)
        
        . egen flag=group(A B C)
        
        . list
        
             +------------------+
             | A   B   C   flag |
             |------------------|
          1. | 2   1   2      2 |
          2. | 2   2   2      3 |
          3. | 2   1   1      1 |
             +------------------+
        
        . g D=1 if flag==3
        (2 missing values generated)
        
        .
        Kind regards,
        Carlo
        (Stata 19.0)

        Comment


        • #5
          Alternatively:
          Code:
          egen miss = rowmiss(smoking_*)
          gen var1 = smoking_a==2 | smoking_b==2 | smoking_c==2 if miss!=3
          drop miss
          * OR
          gen var1 = cond(smoking_a==2 | smoking_b==2 | smoking_c==2, 1, cond(missing(smoking_a) & missing(smoking_b) & missing(smoking_c), ., 0))

          Comment


          • #6
            Excellent all sorted!

            Comment


            • #7
              Or, perhaps not shortest code, but perhaps most simplistically:
              Code:
              generate var1=0
              replace var1=1 if smoking_a==2 | smoking_b==2 | smoking_c==2
              replace var1=. if smoking_a==. & smoking_b==. & smoking_c==.

              Comment


              • #8
                Note that if a variable is 2 it is certainly not missing so conditions like

                Code:
                smoking_a == 2 & smoking_a !=.
                just collapse to conditions like
                Code:
                  
                smoking_a == 2

                Comment

                Working...
                X