0

I have a panel data set of unemployment numbers over time for a series of countries. I have generated a variable that is equal to 1 if the unemployment rate is above 20% and I want to count the consecutive years that unemployment is above that rate. Some countries drop out of the data for some years as there were no events that year but i would like the count to include the missing year if possible I included an example of this. I have been able to create a straight sum of the highunemp variable for each country but not the consecutive years that I need. Here is an example of my data:

Country|Year |Unemprat| highunemp
-------|-----|--------|-------
      1| 2001| 15     |0
      2| 2001| 25     |1
      3| 2001| 40     |1
      1| 2002| 20     |1
      2| 2002| 25     |1
      3| 2002| 32     |1
      4| 2002| 8      |0
      1| 2003| 14     |0
      3| 2003| 22     |1
      4| 2003| 26     |1
      1| 2004| 23     |1
      2| 2004| 25     |1
      3| 2004| 10     |0
      4| 2004| 14     |0

Im looking for a dataset like this with the added variable for consecutive years

Country|Year |Unemprat| highunemp | conshigh
-------|-----|--------|-----------|---------
      1| 2001| 15     |0          |0
      2| 2001| 25     |1          |1
      3| 2001| 40     |1          |1
      1| 2002| 20     |1          |1
      2| 2002| 25     |1          |2
      3| 2002| 32     |1          |2
      4| 2002| 8      |0          |0
      1| 2003| 14     |0          |0
      3| 2003| 22     |1          |3
      4| 2003| 26     |1          |1
      1| 2004| 23     |1          |1
      2| 2004| 25     |1          |4
      3| 2004| 10     |0          |0
      4| 2004| 14     |0          |0

Ideally I am hoping to do this in Stata as that is where I have been working with my data currently but I can also work with R.

2
  • Why don't you include Country 4 in 2001? It's missing...
    – pogibas
    Sep 30, 2017 at 22:06
  • So this is just an example but my data set in its current form is a panel data set that counts the number of events in a given country per year but since it is a collapse of a larger data set that was each individual event. Any year where a country does not have an event is effectively lost in the new data set (this is its own issue Im trying to deal with so if you have a suggestion on that its also welcome). In essence I was simulating as if country 4 had no events in 2001 but does have events in the following years. Similar to country 2 missing events in 2003 which is not uncommon in my set. Sep 30, 2017 at 22:32

3 Answers 3

1

How about this?

input country year unemp
1 2001 15
2 2001 25
3 2001 40
1 2002 20
2 2002 25
3 2002 32
4 2002 8
1 2003 14
3 2003 22
4 2003 26
1 2004 23
2 2004 25
3 2004 10
4 2004 14
end
gen high = unem >= 20
fillin country year
xtset country year
gen cons = high==1 & l.high==1
by country: gen cumc = sum(cons)
by country: replace cons = -l.cumc if high!=1
by country: replace cumc = sum(cons)
gen conshigh = high+cumc
l, sep(4)
* clean up
drop if _fillin
drop _fillin cons cumc
sort year country

Here is the output of l, sep(4) before *cleanup:

     +------------------------------------------------------------------+
     | country   year   unemp   high   _fillin   cons   cumc   conshigh |
     |------------------------------------------------------------------|
  1. |       1   2001      15      0         0      .      0          0 |
  2. |       1   2002      20      1         0      0      0          1 |
  3. |       1   2003      14      0         0      0      0          0 |
  4. |       1   2004      23      1         0      0      0          1 |
     |------------------------------------------------------------------|
  5. |       2   2001      25      1         0      0      0          1 |
  6. |       2   2002      25      1         0      1      1          2 |
  7. |       2   2003       .      .         1     -1      0          . |
  8. |       2   2004      25      1         0      0      0          1 |
     |------------------------------------------------------------------|
  9. |       3   2001      40      1         0      0      0          1 |
 10. |       3   2002      32      1         0      1      1          2 |
 11. |       3   2003      22      1         0      1      2          3 |
 12. |       3   2004      10      0         0     -2      0          0 |
     |------------------------------------------------------------------|
 13. |       4   2001       .      .         1      .      0          . |
 14. |       4   2002       8      0         0      0      0          0 |
 15. |       4   2003      26      1         0      0      0          1 |
 16. |       4   2004      14      0         0      0      0          0 |
     +------------------------------------------------------------------+

BTW, your Country 2, Year 2004 is strange because Country 2 has 2001=1, 2002=1, 2003=., 2004=1 so the value of conshigh for Year 2004 should be 1, not 4. If you want it to be 4, you can first fill in high for the missing years and then do others. I think it's an issue that is orthogonal to the present technical matter.

Also, it would have been easier to read if you sorted the data by Country and then Year, not the other way.

1

I actually seem to have found an answer in Stata.

by country (year), sort: gen spell = sum(highunemp != highunemp[_n-1])

by country spell (year), sort: gen conshigh = (cond(highunemp, _n, 0))
1

I'm going to suggest a solution, not to the problem in your initial post, but to the problem that you describe in your comment - the gaps in your data, which are complicating your effort to find spells of high unemployment. You should have observations for every country/year combination, with a zero for the event count if there were no events. The code below starts with the country/year combinations you have and uses the fillin command to fill in the missing combinations, and then sets the count to zero in the newly-added observations. Leaving missing observations in your dataset, when you actually know what the values should be, is a mistake that will complicate your analysis (as it appears to have done already). If you go back in your process and correct this, then add unemployment data, your analysis will be in better shape. If you were only given unemployment data for the country/year observations that had events, you should find a source of those rates and merge them to your event data again.

. list, clean

       Country   Year   events  
  1.         1   2001        4  
  2.         2   2001        5  
  3.         3   2001        1  
  4.         1   2002        5  
  5.         2   2002        2  
  6.         3   2002        5  
  7.         4   2002        5  
  8.         1   2003        2  
  9.         3   2003        3  
 10.         4   2003        4  
 11.         1   2004        2  
 12.         2   2004        3  
 13.         3   2004        4  
 14.         4   2004        5  

. fillin Country Year

. replace events = 0 if _fillin
(2 real changes made)

. drop _fillin

. list, clean

       Country   Year   events  
  1.         1   2001        4  
  2.         1   2002        5  
  3.         1   2003        2  
  4.         1   2004        2  
  5.         2   2001        5  
  6.         2   2002        2  
  7.         2   2003        0  
  8.         2   2004        3  
  9.         3   2001        1  
 10.         3   2002        5  
 11.         3   2003        3  
 12.         3   2004        4  
 13.         4   2001        0  
 14.         4   2002        5  
 15.         4   2003        4  
 16.         4   2004        5  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.