There are two dates, start date and end date. I want to know whether any date between start and end date are included in certain date ranges (recession dates)
For example, (start date) (end date)
ID /YEAR /BECAMECEO/ LEFTCEO
00003| 1992 | 01dec1989 | 31dec1998
00003| 2001 | 01apr2001 | 30oct2009
00006| 1992 | 01nov1991 | 15feb2001
00006| 2007 | 02jul2007 | 31mar2008
00008| 1996 | 01may1996| 28dec2000
00026| 2002 | 03jul2001 | 01feb2002
I want to know, for each ID, whether any day of CEO duration period (any date between start date to end date) is included in the ranges of recession dates and if so, how many ranges. If each ID can work multiple times and work dates can be included in multiple ranges
((FOR EXAMPLE)) for ID 00003, she worked twice during (01dec1989 - 31dec1998 & 01apr2001 - 30oct2009) and dates within this period are included in three recession ranges as shown below (i.e. recession ranges of (1) July 1990-March 1991 / (2) March 2001 to Nov 2001/ and (3) dec 2007 to June 2009) , I want result value "recession_ceo==3"
For ID 00006, working twice during (01nov1991- 15feb2001 & 02jul2007 - 31mar2008), and working dates are included in one range of recession (dec 2007 to june 2009) and I want result value "recession_ceo==1"
For ID 00008, she worked during (01may1996 - 28dec2000) and dates are not included in any range of recession, thus "recession_ceo ==0"
For ID 00026, working during 03jul2001- 01feb2002, thus one recession range is included (mar2001 to nov 2001), thus "recession_ceo==1"
Recession dates Ranges:
My coding idea is, for each ID, starting from BECAMECEO, check whether it is in the recession ranges. If yes, then result value (recesssion_ceo ==1). Otherwise, keep adding the date until end date (LEFTOFC) and check any date is included in recession ranges. If none of date between start and end date is included in range, then result value of recession_ceo ==0. and then end the loop.
gen recession_ceo =0
gen n =LEFTOFC-BECAMECEO
forval i = 1/n {
if betweendate+i = ( inrange(BECAMECEO, td(01aug1929),td(31mar1933)) | inrange(BECAMECEO, td(01may1937) , td(30jun1938)) | inrange(BECAMECEO, td(01feb1945), td(31oct1945)) | inrange(BECAMECEO,td(01nov1948), td(31oct1949)) | inrange(BECAMECEO, td(01jul1953) , td(31may1954)) | inrange(BECAMECEO, td(01aug1957), td(30apr1958)) | inrange(BECAMECEO, td(01apr1960), td(28feb1961)) | inrange(BECAMECEO, td(01dec1969), td(30nov1970)) | inrange(BECAMECEO,td(01nov1973), td(31mar1975)) | inrange(BECAMECEO, td(01jan1980), td(31jul1980)) | inrange(BECAMECEO, td(01jul1981), td(30nov1982)) | inrange(BECAMECEO, td(01jul1990), td(31mar1991)) | inrange(BECAMECEO, td(01mar2001) , td(30nov2001)) | inrange(BECAMECEO, td(01dec2007) , td(30jun2009)) | inrange(BECAMECEO, td(01feb2020) , td(30apr2020))
{replace recession_ceo==1}
else {recession_ceo==0}
}
For example, (start date) (end date)
ID /YEAR /BECAMECEO/ LEFTCEO
00003| 1992 | 01dec1989 | 31dec1998
00003| 2001 | 01apr2001 | 30oct2009
00006| 1992 | 01nov1991 | 15feb2001
00006| 2007 | 02jul2007 | 31mar2008
00008| 1996 | 01may1996| 28dec2000
00026| 2002 | 03jul2001 | 01feb2002
I want to know, for each ID, whether any day of CEO duration period (any date between start date to end date) is included in the ranges of recession dates and if so, how many ranges. If each ID can work multiple times and work dates can be included in multiple ranges
((FOR EXAMPLE)) for ID 00003, she worked twice during (01dec1989 - 31dec1998 & 01apr2001 - 30oct2009) and dates within this period are included in three recession ranges as shown below (i.e. recession ranges of (1) July 1990-March 1991 / (2) March 2001 to Nov 2001/ and (3) dec 2007 to June 2009) , I want result value "recession_ceo==3"
For ID 00006, working twice during (01nov1991- 15feb2001 & 02jul2007 - 31mar2008), and working dates are included in one range of recession (dec 2007 to june 2009) and I want result value "recession_ceo==1"
For ID 00008, she worked during (01may1996 - 28dec2000) and dates are not included in any range of recession, thus "recession_ceo ==0"
For ID 00026, working during 03jul2001- 01feb2002, thus one recession range is included (mar2001 to nov 2001), thus "recession_ceo==1"
Recession dates Ranges:
| August 1929 ~ | March 1933 |
| May 1937 ~ | June 1938 |
| February 1945 ~ | October 1945 |
| November 1948 ~ | October 1949 |
| July 1953 ~ | May 1954 |
| August 1957 ~ | April 1958 |
| April 1960 ~ | February 1961 |
| December 1969 ~ | November 1970 |
| November 1973 ~ | March 1975 |
| January 1980 ~ | July 1980 ( |
| July 1981 ~ | November 1982 |
| July 1990 ~ | March 1991 |
| March 2001 ~ | November 2001 |
| December 2007 ~ | June 2009 |
| February 2020 ~ | April 2020 |
My coding idea is, for each ID, starting from BECAMECEO, check whether it is in the recession ranges. If yes, then result value (recesssion_ceo ==1). Otherwise, keep adding the date until end date (LEFTOFC) and check any date is included in recession ranges. If none of date between start and end date is included in range, then result value of recession_ceo ==0. and then end the loop.
gen recession_ceo =0
gen n =LEFTOFC-BECAMECEO
forval i = 1/n {
if betweendate+i = ( inrange(BECAMECEO, td(01aug1929),td(31mar1933)) | inrange(BECAMECEO, td(01may1937) , td(30jun1938)) | inrange(BECAMECEO, td(01feb1945), td(31oct1945)) | inrange(BECAMECEO,td(01nov1948), td(31oct1949)) | inrange(BECAMECEO, td(01jul1953) , td(31may1954)) | inrange(BECAMECEO, td(01aug1957), td(30apr1958)) | inrange(BECAMECEO, td(01apr1960), td(28feb1961)) | inrange(BECAMECEO, td(01dec1969), td(30nov1970)) | inrange(BECAMECEO,td(01nov1973), td(31mar1975)) | inrange(BECAMECEO, td(01jan1980), td(31jul1980)) | inrange(BECAMECEO, td(01jul1981), td(30nov1982)) | inrange(BECAMECEO, td(01jul1990), td(31mar1991)) | inrange(BECAMECEO, td(01mar2001) , td(30nov2001)) | inrange(BECAMECEO, td(01dec2007) , td(30jun2009)) | inrange(BECAMECEO, td(01feb2020) , td(30apr2020))
{replace recession_ceo==1}
else {recession_ceo==0}
}

Comment