Announcement

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

  • Conditional 'if'

    Dear Statalist users,

    I have a panel dataset from 1974-2014 set to xtreg on Stata 14. I have created a dummy variable ("suffrage_dummy") that takes the value 1 for the year of the introduction of universal suffrage (between this period 1974-2014). I have generated another dummy variable ("suffrage_dummy2") that takes the same value as "suffrage_dummy". I want to replace the values of this "suffrage_dummy2" variable with value 1 conditional on two criteria:

    1. if the year is higher than the establishment of universal suffrage AND
    2. if the average in range (for those years higher than the one when the suffrage is introduced) is equal to 1.

    For example,

    Code:
    year    country    v2x_suffr    suffrage_dummy    suffrage_dummy2
    1994    Palestine/West Bank    0    0    0
    1995    Palestine/West Bank    0    0    0
    1996    Palestine/West Bank    1    1    1
    1997    Palestine/West Bank    1    0    0
    1998    Palestine/West Bank    1    0    0
    1999    Palestine/West Bank    1    0    0
    2000    Palestine/West Bank    1    0    0
    2001    Palestine/West Bank    1    0    0
    2002    Palestine/West Bank    1    0    0
    2003    Palestine/West Bank    1    0    0
    2004    Palestine/West Bank    1    0    0
    2005    Palestine/West Bank    1    0    0
    2006    Palestine/West Bank    1    0    0
    2007    Palestine/West Bank    1    0    0
    2008    Palestine/West Bank    1    0    0
    2009    Palestine/West Bank    1    0    0
    2010    Palestine/West Bank    1    0    0
    2011    Palestine/West Bank    1    0    0
    2012    Palestine/West Bank    1    0    0
    2013    Palestine/West Bank    1    0    0
    2014    Palestine/West Bank    1    0    0
    In this example, Palestine introduced universal suffrage in 1996 (look at the value of "v2x_suffr"), which remained until 2014, the last year of observation in my dataset. Since the years 1997-2014 are higher than 1996 (condition 1 above fulfilled) and the average value of suffrage in this period is equal to 1 (condition 2 fulfilled), the value of "suffrage_dummy2" would take the value of 1.

    In this other illustration below, Bhutan introduced universal suffrage (look at the value of "v2x_suffr") in 2007 but in 2014, reduced it to 0.98. Therefore, the value for "suffrage_dummy2" should take on the value of 0 for the years 2008-2014 since the average suffrage in those years are less than 1 (owing to the value of 0.98 in 2014):

    Code:
    year    country    v2x_suffr    suffrage_dummy    suffrage_dummy2
    2006    Bhutan    0    0    0
    2007    Bhutan    0    0    0
    2008    Bhutan    1    1    1
    2009    Bhutan    1    0    0
    2010    Bhutan    1    0    0
    2011    Bhutan    1    0    0
    2012    Bhutan    1    0    0
    2013    Bhutan    1    0    0
    2014    Bhutan    .98    0    0
    The trick is that not all countries who gave universal suffrage in this period 1974-2014 introduced universal suffrage at the same time. I have been looking at these two sites for inspiration but I am unable to crack it as yet:

    HTML Code:
    https://www.stata.com/statalist/archive/2004-02/msg00759.html#
    HTML Code:
    https://stats.idre.ucla.edu/stata/modules/creating-and-recoding-variables/
    Any help will be much appreciated.

    Ashvinder
    Last edited by Ashvinder Singh; 26 Jul 2017, 05:10.

  • #2
    I'm having trouble following your description, but I think what you need is a variable that indicates the period or spell within which suffrage is not zero so that you can calculate your average by panel (using the -bysort- prefix). I added another example to your countries to show you how this rule applies to nations where suffrage starts/stops but I'm still not sure that I answered your question:


    Code:
    clear
    input year   str50 country    v2x_suffr    suffrage_dummy    suffrage_dummy2
    1994   "Palestine/West Bank"   0    0    0
    1995   "Palestine/West Bank"   0    0    0
    1996   "Palestine/West Bank"   1    1    1
    1997   "Palestine/West Bank"   1    0    0
    1998   "Palestine/West Bank"   1    0    0
    1999   "Palestine/West Bank"   1    0    0
    2000   "Palestine/West Bank"   1    0    0
    2001   "Palestine/West Bank"   1    0    0
    2002   "Palestine/West Bank"   1    0    0
    2003   "Palestine/West Bank"   1    0    0
    2004   "Palestine/West Bank"   1    0    0
    2005   "Palestine/West Bank"   1    0    0
    2006   "Palestine/West Bank"   1    0    0
    2007   "Palestine/West Bank"   1    0    0
    2008   "Palestine/West Bank"   1    0    0
    2009   "Palestine/West Bank"   1    0    0
    2010   "Palestine/West Bank"   1    0    0
    2011   "Palestine/West Bank"   1    0    0
    2012   "Palestine/West Bank"   1    0    0
    2013   "Palestine/West Bank"   1    0    0
    2014   "Palestine/West Bank"   1    0    0
    2006   "Bhutan"   0    0    0
    2007   "Bhutan"   0    0    0
    2008   "Bhutan"   1    1    1
    2009   "Bhutan"   1    0    0
    2010   "Bhutan"   1    0    0
    2011   "Bhutan"   1    0    0
    2012   "Bhutan"   1    0    0
    2013   "Bhutan"   1    0    0
    2014   "Bhutan"   .98    0    0
    2001   "test"     0 0 0
    2002   "test"     1 1 0
    2003   "test"     .5 0 0
    2004   "test"     0 0 0
    2005   "test"     0 0 0
    2008   "test"     0 0 0
    2009   "test"     1 1 0
    2010   "test"     1.1 0 0
    2011   "test"     0 0 0
    end
    
    
    bys country (year): g startstop = _n if v2x_suffr!=0
    
    bys country (year): replace startstop = startstop[_n-1] if !mi(startstop[_n-1]) & !mi(startstop[_n])
        
    
      bys country startstop (year): egen avg = mean(v2x_suffr) 
      
    
    cap drop suffrage_dummy2
    g suffrage_dummy2 = 0
    replace suffrage_dummy2 = 1 if avg>=1
    
    sort coun year
    Eric A. Booth | Senior Director of Research | Far Harbor | Austin TX

    Comment


    • #3
      Dear Eric,

      Many, many thanks for the codes.

      The codes are almost right. However, for countries that have already introduced universal suffrage from the start of my dataset (i.e. 1974), they too would have to be coded as 0 (i.e. I am trying to measure the jump from 0 to 1 in "v2x_suffr" between 1974 to 2014. So if "v2x_suffr" is already 1 at 1974, there is no 'jump' per se). At the moment, stata codes them as 1.

      So I reckon that every line of code is right until probably this one:

      Code:
      replace suffrage_dummy2 = 1 if avg>=1
      Ashvinder
      Last edited by Ashvinder Singh; 26 Jul 2017, 07:29.

      Comment

      Working...
      X