Announcement

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

  • Creating dummy variable in Panel Data

    Hi, i want to create a dummy variable (group-wise). my data ranges between 2001 to 2010. Features of the dummy variables are below:
    Condition 1. the dummy variable equals one if variable AQA value for firm i in year t is non-missing or non-zero
    Condition 2. in condition 1, for example AQA value is non-zero in year 2004 for firm i, then i want dummy variable for firm i equals one from year 2004 to end year 2005, 2006, 2007, 2008, 2009, and 2010.

  • #2
    Obaid:
    without any data excerpt (please see -dataex-) I find difficult to sketch something more than a temptative reply (by the way: my guess is that your 2 conditions may contradict each other in some instances).
    Anyway, I do hope that the following toy-example can be helpful:
    Code:
    . set obs 4
    number of observations (_N) was 0, now 4
    
    . g id=1
    
    . g year=2010+_n
    
    . g AQA=0 in 1
    
    . replace AQA=0 in 3
    
    . replace AQA=runiform() in 2
    
    . expand 2
    
    . replace id=2 in 5/8
    
    . replace AQA=runiform() in 5/8
    
    . bysort year: gen dummy1=1 if AQA!=0 & AQA!=.
    
    . sort id year
    
    . list
    
        +-------------------------------+
         | id   year        AQA   dummy1 |
         |-------------------------------|
      1. |  1   2011          0        . |
      2. |  1   2012   .3488717        1 |
      3. |  1   2013          0        . |
      4. |  1   2014          .        . |
      5. |  2   2011   .2668857        1 |
         |-------------------------------|
      6. |  2   2012   .1366463        1 |
      7. |  2   2013   .0285569        1 |
      8. |  2   2014   .8689333        1 |
         +-------------------------------+
    
    .
    Kind regards,
    Carlo
    (Stata 18.0 SE)

    Comment


    • #3
      Thanks Carlo

      The following picture show what i exactly need

      the dummy variable 'AOA' capture 1 when x1 has non-zero value. Also the AOA keep one for rest of its observations for each "sn".

      note: following is example data set. The original dataset is unstructured panel.
      sn year x1 AOA
      1 2002 0
      1 2003 0.614976 1
      1 2004 . 1
      2 2002 0 0
      2 2003 0 0
      2 2004 . 0
      3 2002 0.038193 1
      3 2003 0 1
      3 2004 0 1
      4 2002 . 0
      4 2003 . 0
      4 2004 0.141908 1
      5 2002 0.845105 1
      5 2003 . 1
      5 2004 1
      6 2002 0.37749 1
      6 2003 .. 1
      6 2004 0.83081 1
      Currently I'm using following command:

      Code:
      gen AOA = 0
      replace AOA = 1 if x1 != 0 & x1 != .
      but i don't know how expand one to rest of observation for individual "sn" in a panel dataset.

      Comment


      • #4
        Currently, I have the following output:
        Code:
        set obs 5
        number of observations (_N) was 0, now 5
        
        . gen id = 1
        
        . set obs 10
        number of observations (_N) was 5, now 10
        
        . replace id = 2 if id == .
        (5 real changes made)
        
        . set obs 15
        number of observations (_N) was 10, now 15
        
        . replace id = 3 if id == .
        (5 real changes made)
        
        . set obs 20
        number of observations (_N) was 15, now 20
        
        . replace id = 4 if id == .
        (5 real changes made)
        
        . bysort id: gen year = 2010+_n
        
        . gen AQA = 0
        
        . replace AQA=runiform() in 2 if id == 1
        (1 real change made)
        
        . replace AQA=runiform() in 4 if id == 2
        (0 real changes made)
        
        . replace AQA=runiform() in 9 if id == 2
        (1 real change made)
        
        . replace AQA=runiform() in 13 if id == 3
        (1 real change made)
        
        . replace AQA=runiform() in 16 if id == 4
        (1 real change made)
        
        . gen dummy = 0
        
        . replace dummy = 1 in 2 if id == 1
        (1 real change made)
        
        . replace dummy = 1 in 9 if id == 2
        (1 real change made)
        
        . replace dummy = 1 in 13 if id == 3
        (1 real change made)
        
        . replace dummy = 1 in 16 if id == 4
        (1 real change made)
        
        . sort id year
        
        . list
        
             +------------------------------+
             | id   year        AQA   dummy |
             |------------------------------|
          1. |  1   2011          0       0 |
          2. |  1   2012   .8689333       1 |
          3. |  1   2013          0       0 |
          4. |  1   2014          0       0 |
          5. |  1   2015          0       0 |
             |------------------------------|
          6. |  2   2011          0       0 |
          7. |  2   2012          0       0 |
          8. |  2   2013          0       0 |
          9. |  2   2014   .3508549       1 |
         10. |  2   2015          0       0 |
             |------------------------------|
         11. |  3   2011          0       0 |
         12. |  3   2012          0       0 |
         13. |  3   2013   .0711051       1 |
         14. |  3   2014          0       0 |
         15. |  3   2015          0       0 |
             |------------------------------|
         16. |  4   2011    .323368       1 |
         17. |  4   2012          0       0 |
         18. |  4   2013          0       0 |
         19. |  4   2014          0       0 |
         20. |  4   2015          0       0 |
             +------------------------------+
        Following is desired result for my large panel dataset. thanks

        Code:
        replace dummy = 1 in 3 if id == 1
        (1 real change made)
        
        . replace dummy = 1 in 4 if id == 1
        (1 real change made)
        
        . replace dummy = 1 in 5 if id == 1
        (1 real change made)
        
        . replace dummy = 1 in 10 if id == 2
        (1 real change made)
        
        . replace dummy = 1 in 14 if id == 3
        (1 real change made)
        
        . replace dummy = 1 in 15 if id == 3
        (1 real change made)
        
        . replace dummy = 1 in 17 if id == 4
        (1 real change made)
        
        . replace dummy = 1 in 18 if id == 4
        (1 real change made)
        
        . replace dummy = 1 in 19 if id == 4
        (1 real change made)
        
        . replace dummy = 1 in 20 if id == 4
        (1 real change made)
        
        . sort id year
        
        . list
        
             +------------------------------+
             | id   year        AQA   dummy |
             |------------------------------|
          1. |  1   2011          0       0 |
          2. |  1   2012   .8689333       1 |
          3. |  1   2013          0       1 |
          4. |  1   2014          0       1 |
          5. |  1   2015          0       1 |
             |------------------------------|
          6. |  2   2011          0       0 |
          7. |  2   2012          0       0 |
          8. |  2   2013          0       0 |
          9. |  2   2014   .3508549       1 |
         10. |  2   2015          0       1 |
             |------------------------------|
         11. |  3   2011          0       0 |
         12. |  3   2012          0       0 |
         13. |  3   2013   .0711051       1 |
         14. |  3   2014          0       1 |
         15. |  3   2015          0       1 |
             |------------------------------|
         16. |  4   2011    .323368       1 |
         17. |  4   2012          0       1 |
         18. |  4   2013          0       1 |
         19. |  4   2014          0       1 |
         20. |  4   2015          0       1 |
             +------------------------------+

        Comment

        Working...
        X