Announcement

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

  • Creating a dummy variable based on three other variables

    I have a variable year (2003 and 2013), an export (dummy 0 and 1) and an idcode. I would like to create a dummy as follows: if for an idcode in 2003 export is 0 but in 2013 it is 1 then the dummy would be 1. For example:

    Code:
    year    export       idcode
    2003       1         6000146
    2003       0         3047347
    2013       1         6000146
    2013       1         3047347
    Then, the new base will be:

    Code:
    year     export        idcode        dummy
    2003        1          6000146         0
    2003        0          3047347         0
    2013        1          6000146         0
    2013        1          3047347         1
    Can anybody help me? Thanks in advance for your attention!
    Last edited by Alexandre Loures; 09 Nov 2018, 06:31.

  • #2
    Code:
    clear 
    input year    export       idcode
    2003       1         6000146
    2003       0         3047347
    2013       1         6000146
    2013       1         3047347
    end 
    
    egen wanted = total((year == 2003 & export == 0) | (year == 2013 & export == 1)), by(id)
    
    sort idcode year
    
    list, sepby(idcode) 
    
         +----------------------------------+
         | year   export    idcode   wanted |
         |----------------------------------|
      1. | 2003        0   3047347        2 |
      2. | 2013        1   3047347        2 |
         |----------------------------------|
      3. | 2003        1   6000146        1 |
      4. | 2013        1   6000146        1 |
         +----------------------------------+
    
    replace wanted = wanted == 2 
    
    list, sepby(idcode) 
    
         +----------------------------------+
         | year   export    idcode   wanted |
         |----------------------------------|
      1. | 2003        0   3047347        1 |
      2. | 2013        1   3047347        1 |
         |----------------------------------|
      3. | 2003        1   6000146        0 |
      4. | 2013        1   6000146        0 |
         +----------------------------------+

    Comment

    Working...
    X