Originally posted by KaLeigh White
View Post
"july" in my code is an indicator of whether a policy was active in the month of July. It distributes the 0 or 1 to all observations corresponding to a particular policy. "tag" identifies whether there is a policy active in the month of July, just in case that there are multiple policies that are tied for maximum number of months active. So in my code, it is the combination of july and tag that defines what observations are to be dropped. I highlighted two potential issues with your code in #8 (please reread). If these two issues do not arise, the codes should give you the same results. But my code is "safer" in that it accounts for the issued raised in #8. One more thing about july, as the policies may span over multiple years, we need to add a year restriction. If you are noticing a specific issue with the code, be explicit about what it is.
Code:
* Example generated by -dataex-. For more info, type help dataex
clear
input str14 state int policydatebegin float policydateend
"Mississippi" 13880 14244
"Mississippi" 14426 14517
"Mississippi" 14518 14975
"Mississippi" 15035 15917
"Mississippi" 16223 16952
"Mississippi" 17257 17470
"Mississippi" 17532 17805
"Mississippi" 17898 18382
"Mississippi" 18383 19935
"Mississippi" 20089 21185
"Pennsylvania" 13849 16860
"Pennsylvania" 16861 17074
"Pennsylvania" 17167 17622
"Pennsylvania" 17714 17805
"Pennsylvania" 18079 18109
"Pennsylvania" 18232 19448
"Pennsylvania" 19540 22780
"Virginia" 13880 14244
"Virginia" 14426 14975
"Virginia" 15127 15340
"Virginia" 15341 16252
"Virginia" 16253 17074
"Virginia" 17348 17439
"Virginia" 17440 17805
"Virginia" 18079 18352
"Virginia" 18353 19723
"Virginia" 19724 21730
end
format %td policydatebegin
format %td policydateend
*CHECK IF STATE POLICYDATEBEGIN POLICYDATEEND UNIQUELY IDENTIFY AN OBSERVATION
isid state policydatebegin policydateend
*IF #1 PASSES
egen policyid= group(state policydatebegin policydatebegin)
g startym= ym(year(policydatebegin), month(policydatebegin))
g endym= ym(year(policydateend), month(policydateend))
expand endym-startym+1
bys state policyid (startym): g yearmonth= startym[1]+_n-1
format *ym yearmonth %tm
g year= year(dofm(yearmonth))
bys policyid year: g duration=_N
bys state year: egen maxduration= max(duration)
keep if duration==maxduration
sort state policyid yearmonth
bysort state policyid year: egen july= max(month(dofm(yearmonth))==7)
bysort state policyid (year): keep if _n==1
bysort state year (july): gen tag= july[_N] & !july[1]
drop if !july & tag
l state policyid policydatebegin policydateend year yearmonth july tag, sepby(state year)
Res.:
Code:
. l state policyid policydatebegin policydateend year yearmonth july tag, sepby(state year)
+--------------------------------------------------------------------------------+
| state policyid policyd~n policyd~d year yearmo~h july tag |
|--------------------------------------------------------------------------------|
1. | Mississippi 1 01jan1998 31dec1998 1998 1998m1 1 0 |
|--------------------------------------------------------------------------------|
2. | Mississippi 2 01jul1999 30sep1999 1999 1999m7 1 1 |
|--------------------------------------------------------------------------------|
3. | Mississippi 4 01mar2001 31jul2003 2001 2001m3 1 0 |
|--------------------------------------------------------------------------------|
4. | Mississippi 5 01jun2004 31may2006 2004 2004m6 1 0 |
|--------------------------------------------------------------------------------|
5. | Mississippi 6 01apr2007 31oct2007 2007 2007m4 1 0 |
|--------------------------------------------------------------------------------|
6. | Mississippi 7 01jan2008 30sep2008 2008 2008m1 1 0 |
|--------------------------------------------------------------------------------|
7. | Mississippi 8 01jan2009 30apr2010 2009 2009m1 1 0 |
|--------------------------------------------------------------------------------|
8. | Mississippi 9 01may2010 31jul2014 2010 2010m5 1 0 |
|--------------------------------------------------------------------------------|
9. | Mississippi 10 01jan2015 01jan2018 2015 2015m1 1 0 |
|--------------------------------------------------------------------------------|
10. | Pennsylvania 11 01dec1997 28feb2006 1997 1997m12 0 0 |
|--------------------------------------------------------------------------------|
11. | Pennsylvania 12 01mar2006 30sep2006 2006 2006m3 1 0 |
|--------------------------------------------------------------------------------|
12. | Pennsylvania 13 01jan2007 31mar2008 2007 2007m1 1 0 |
|--------------------------------------------------------------------------------|
13. | Pennsylvania 14 01jul2008 30sep2008 2008 2008m7 1 0 |
|--------------------------------------------------------------------------------|
14. | Pennsylvania 15 01jul2009 31jul2009 2009 2009m7 1 1 |
|--------------------------------------------------------------------------------|
15. | Pennsylvania 17 01jul2013 15may2022 2013 2013m7 1 0 |
|--------------------------------------------------------------------------------|
16. | Virginia 18 01jan1998 31dec1998 1998 1998m1 1 0 |
|--------------------------------------------------------------------------------|
17. | Virginia 19 01jul1999 31dec2000 1999 1999m7 1 0 |
|--------------------------------------------------------------------------------|
18. | Virginia 20 01jun2001 31dec2001 2001 2001m6 1 0 |
|--------------------------------------------------------------------------------|
19. | Virginia 21 01jan2002 30jun2004 2002 2002m1 1 0 |
|--------------------------------------------------------------------------------|
20. | Virginia 22 01jul2004 30sep2006 2004 2004m7 1 0 |
|--------------------------------------------------------------------------------|
21. | Virginia 23 01jul2007 30sep2007 2007 2007m7 1 1 |
|--------------------------------------------------------------------------------|
22. | Virginia 25 01jul2009 31mar2010 2009 2009m7 1 0 |
|--------------------------------------------------------------------------------|
23. | Virginia 26 01apr2010 31dec2013 2010 2010m4 1 0 |
|--------------------------------------------------------------------------------|
24. | Virginia 27 01jan2014 30jun2019 2014 2014m1 1 0 |
+--------------------------------------------------------------------------------+
.

Comment