Announcement

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

  • Dummy varibale with if/condition

    Dear All,

    I have to generate a dummy variable. The only thing that makes it a bit complicated is that I need generate the dummy and then replace some observations based on a condition.

    I have the following data example:


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int year byte(var1 var2) int var3 byte(var4 var5 var6 var7 var8 var9) int(var10 var11 var12 var13) str3 obs
    2013 1 10 3109 31 28 31 29 29 0 2920 3109 2910 3109 " 1"
    2013 1 10 3109 31 29 31 29 29 0 2920 3109 2910 3109 " 2"
    2013 1 10 3109 31 30 31 29 29 0 2920 3109 2910 3109 " 3"
    2013 1 10 3109 31 31 31 29 29 0 2920 3109 2910 3109 " 4"
    2013 1 10 3109 31 32 31 29 29 0 2920 3109 2910 3109 " 5"
    2014 2 20 2920 29 28 31 29 29 0 2920 3109 2910 3109 " 6"
    2014 2 20 2920 29 29 31 29 29 0 2920 3109 2910 3109 " 7"
    2014 2 20 2920 29 30 31 29 29 0 2920 3109 2910 3109 " 8"
    2014 2 20 2920 29 31 31 29 29 0 2920 3109 2910 3109 " 9"
    2014 2 20 2920 29 32 31 29 29 0 2920 3109 2910 3109 "10"
    2015 3 30 2910 29 28 31 29 29 0 2920 3109 2910 3109 "11"
    2015 3 30 2910 29 29 31 29 29 0 2920 3109 2910 3109 "12"
    2015 3 30 2910 29 30 31 29 29 0 2920 3109 2910 3109 "13"
    2015 3 30 2910 29 31 31 29 29 0 2920 3109 2910 3109 "14"
    2015 3 30 2910 29 32 31 29 29 0 2920 3109 2910 3109 "15"
    end

    var9 is a dummy that must be equal to 1 if var5 is equal to any of var6, var7, or var8.

    I run this code and I get this data:
    Code:
    replace var9=1 if var5== var6| var5== var7| var5== var8
    replace var9=0 if var9==.


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int year byte(var1 var2) int var3 byte(var4 var5 var6 var7 var8 var9) int(var10 var11 var12 var13) str3 obs
    2013 1 10 3109 31 28 31 29 29 0 2920 3109 2910 3109 " 1"
    2013 1 10 3109 31 29 31 29 29 1 2920 3109 2910 3109 " 2"
    2013 1 10 3109 31 30 31 29 29 0 2920 3109 2910 3109 " 3"
    2013 1 10 3109 31 31 31 29 29 1 2920 3109 2910 3109 " 4"
    2013 1 10 3109 31 32 31 29 29 1 2920 3109 2910 3109 " 5"
    2014 2 20 2920 29 28 31 29 29 0 2920 3109 2910 3109 " 6"
    2014 2 20 2920 29 29 31 29 29 1 2920 3109 2910 3109 " 7"
    2014 2 20 2920 29 30 31 29 29 0 2920 3109 2910 3109 " 8"
    2014 2 20 2920 29 31 31 29 29 1 2920 3109 2910 3109 " 9"
    2014 2 20 2920 29 32 31 29 29 0 2920 3109 2910 3109 "10"
    2015 3 30 2910 29 28 31 29 29 0 2920 3109 2910 3109 "11"
    2015 3 30 2910 29 29 31 29 29 1 2920 3109 2910 3109 "12"
    2015 3 30 2910 29 30 31 29 29 0 2920 3109 2910 3109 "13"
    2015 3 30 2910 29 31 31 29 29 1 2920 3109 2910 3109 "14"
    2015 3 30 2910 29 32 31 29 29 0 2920 3109 2910 3109 "15"
    end


    In observation (obs) numbers 2, 5, 9, and 14 I have var4!=var5. This is fine.

    what I need to change is in observation (obs) numbers 4, 7, and 12 for example, I have var4=var5. Here var9 is equal to 1.
    • In obs number 12, var9 must not be changed. This is because var4, is within var10, var11, and var12. In other words, it is within two different 4digit-numbers 2910 and 2920, it starts with the same 2-digit numbers (29) and ends with two different 2digit-numbers (10 and 20).
    • The other case is in observation number 4: here I have var4=var5, but var4 is not within more than one 4digit-number in var10, var11, and var12. This means var11 and var13 start with the same two-digit but has the third and the fourth digits similar: they must be different. Var9 in this observation (#4) must be equal to zero.

    How can I replace var9 according to the mentioned condition?

    Thank you,
Working...
X