Announcement

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

  • generate dummy variable with two if conditions

    Hello everyone,

    i have a data set with the two dummy variables date4 and statementdum. I want to create a new dummy variable which is 1 if date4=1 and statementdum=1 and 0 if date 4=1 and statementdum=0.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(date4 statementdum)
    1 1
    0 1
    0 1
    0 1
    0 0
    1 0
    0 1
    0 0
    0 0
    0 1
    end

    I have already tried this code, but it gives the new dummy variable a 1 if one of the two conditions is true.

    gen date4k=.
    replace date4k=1 if date4==1
    replace date4k=1 if statementdum==1
    replace date4k=0 if date4==0
    replace date4k=0 if statementdum==1



    many greetings
    Thomas
    Last edited by Thomas Hall; 21 Nov 2020, 01:26.

  • #2
    You haven't exhausted the possibilities in you specification.

    Maybe start with something like this
    Code:
    generate byte date4k = date4 & statementdum if !mi(date4, statementdum)
    then take a look and go from there to flesh it out the way you want.

    Comment


    • #3
      I found another way for the right solution:
      Code:
      gen date4k=1 if date4==1 & knowledgedum==1
      replace date4k=0 if date4==1 & knowledgedum==0
      thanks for your help Joseph

      Comment


      • #4
        Code:
        gen date4k = knowledgedum if date4 == 1
        is one line not two.

        Comment

        Working...
        X