Announcement

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

  • #16
    Hey great people!

    Kindly assist me in the below. I am using KDHS 2022 and stuck in the month-by-month analysis of teenage births pre and post COVID-19.

    Given b1_01 to b1_20 as month of birth and b2_01 to b2_20 as year of birth and v010 (respondent's year of birth), I intend to create the count of teenage births month-by-month to be able to show a twoway scatter graph and later on an event study model typical to the ones attached. In the twoway scatter for example, I would like to show the count of teenage births on the y-axis against the different months from Jan 2017 to July 2022 with only months Jan and July in a specific year indicated such that my 1st point shows Jan 2017, 2nd is July 2017, 3rd is Jan 2018 etc. And, I'd still like to show the other months as the connecting dots.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(b1_01 b1_02 b1_03 b1_04 b1_05) int(b2_01 b2_02 b2_03 b2_04 b2_05 v010)
     8 12 10  6  . 2021 2013 2010 2009    . 1987
    12  .  .  .  . 2005    .    .    .    . 1983
    10  .  .  .  . 2010    .    .    .    . 1989
     4 10 11  5  9 2019 2016 2009 2007 2002 1982
     8  2  .  .  . 2016 2012    .    .    . 1992
     6  6 11  .  . 2017 2015 2012    .    . 1993
     7  7  7  .  . 2013 2008 2008    .    . 1985
     7  5  .  .  . 2012 2009    .    .    . 1990
     7  .  .  .  . 2018    .    .    .    . 1992
     5 11  .  .  . 2006 1999    .    .    . 1982
    12 12  .  .  . 2021 2019    .    .    . 1987
     8  .  .  .  . 2019    .    .    .    . 1997
     8  2  8  .  . 2014 2008 2006    .    . 1983
     .  .  .  .  .    .    .    .    .    . 2006
     3 11 11  .  . 2014 2010 1998    .    . 1981
     4  .  .  .  . 2019    .    .    .    . 1993
     6 11  .  .  . 2014 2007    .    .    . 1983
    10  8  3  .  . 2015 2014 2013    .    . 1989
    11  8  8  .  . 2013 2003 1996    .    . 1978
     5  .  .  .  . 2012    .    .    .    . 1990
     7  9  .  .  . 2013 2006    .    .    . 1984
     4  2  5  2  . 2020 2015 2013 2012    . 1995
    10 11 12  4  . 2017 2014 2013 2011    . 1984
     .  .  .  .  .    .    .    .    .    . 1997
     5  .  .  .  . 2016    .    .    .    . 1995
     .  .  .  .  .    .    .    .    .    . 1983
     .  .  .  .  .    .    .    .    .    . 1997
     6  .  .  .  . 2018    .    .    .    . 1989
     6 12  .  .  . 2019 2014    .    .    . 1995
    11  .  .  .  . 2018    .    .    .    . 1994
     9  .  .  .  . 2021    .    .    .    . 1994
     8  6  .  .  . 2015 2003    .    .    . 1989
     4  6  3  4  1 2022 2018 2016 2014 2013 1988
     .  .  .  .  .    .    .    .    .    . 2007
    12  6  8  .  . 2012 2009 2004    .    . 1984
     2  3  .  .  . 2020 2013    .    .    . 1992
     6  .  .  .  . 2003    .    .    .    . 1984
     1  .  .  .  . 2019    .    .    .    . 1995
     3  3  1  .  . 2014 2014 2007    .    . 1985
     7  5  9  6  . 2021 2017 2015 2006    . 1988
     9 12  6  .  . 2011 2008 2004    .    . 1986
     1  7  .  .  . 2022 2016    .    .    . 1995
     4  9  .  .  . 2018 2015    .    .    . 1986
     4  9  7  .  . 2017 2012 2008    .    . 1986
    12  4  4  .  . 2012 2010 2010    .    . 1991
     .  .  .  .  .    .    .    .    .    . 2001
    12  7  .  .  . 2006 1998    .    .    . 1975
     .  .  .  .  .    .    .    .    .    . 1998
     .  .  .  .  .    .    .    .    .    . 1994
     5  6  .  .  . 2008 2003    .    .    . 1972
    12 11  .  .  . 2018 2013    .    .    . 1989
     .  .  .  .  .    .    .    .    .    . 2003
     .  .  .  .  .    .    .    .    .    . 1997
     7 12  7 10  . 2020 2008 2005 2003    . 1986
     4 10  .  .  . 2020 2017    .    .    . 1997
     1  3  2 11  7 2017 2011 2009 2006 2003 1976
     7  4  .  .  . 2013 2012    .    .    . 1984
     5  5  .  .  . 2021 2020    .    .    . 1996
     1  4  4  9  . 2022 2017 2015 2012    . 1992
     6  .  .  .  . 2021    .    .    .    . 1998
    10 10  7  .  . 2015 2010 2006    .    . 1984
     .  .  .  .  .    .    .    .    .    . 2006
     7  7  4  .  . 2020 2020 2017    .    . 1994
     5  .  .  .  . 2018    .    .    .    . 1997
    10  4  .  .  . 2021 2018    .    .    . 1990
     4  .  .  .  . 2016    .    .    .    . 1992
     5  3  .  .  . 2017 2013    .    .    . 1993
     5  5  9  1  . 2019 2018 2014 2012    . 1989
     5  .  .  .  . 2021    .    .    .    . 1998
     6 12  .  .  . 2012 1997    .    .    . 1978
     2  .  .  .  . 2022    .    .    .    . 1998
     9  8  .  .  . 2012 2006    .    .    . 1982
     8  8  .  .  . 2016 2012    .    .    . 1994
     3  3  3  .  . 2019 2014 2009    .    . 1990
     1  .  .  .  . 2019    .    .    .    . 1996
     6  4  3  9  . 2016 2014 2012 2009    . 1992
     5  8  5  .  . 2021 2019 2013    .    . 1992
     4  .  .  .  . 2017    .    .    .    . 1994
     8  5 11 11  5 2008 2005 2003 1995 1994 1980
    10  2  3  4  8 2011 2007 2005 2003 2000 1979
     5  .  .  .  . 2022    .    .    .    . 2000
     .  .  .  .  .    .    .    .    .    . 2005
     5  4  .  .  . 2018 2016    .    .    . 1995
     2  8  8  6  . 2020 2008 1998 1996    . 1975
     .  .  .  .  .    .    .    .    .    . 1996
     2  .  .  .  . 2021    .    .    .    . 1995
    12  1  6  .  . 2019 2018 2016    .    . 1996
     1  2 11  .  . 2013 2012 2000    .    . 1981
     .  .  .  .  .    .    .    .    .    . 2000
    10 12 11  8  . 2017 2000 1996 1992    . 1974
    12 12 12  .  . 2018 2015 2015    .    . 1993
    12 10 10  6  . 2018 2017 2011 2008    . 1990
     9 12  8  .  . 2020 2016 2014    .    . 1996
     6  6  7  .  . 2013 2010 2006    .    . 1988
    10  6  6  9 12 2019 2013 2011 2009 2007 1987
    11  .  .  .  . 2002    .    .    .    . 1981
    11  .  .  .  . 2014    .    .    .    . 1997
     .  .  .  .  .    .    .    .    .    . 1996
    10  .  .  .  . 2020    .    .    .    . 1992
     3  8  .  .  . 2015 2005    .    .    . 1989
    end

    Attached Files

    Comment


    • #17
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input byte(b1_01 b1_02 b1_03 b1_04 b1_05) int(b2_01 b2_02 b2_03 b2_04 b2_05 v010)
       8 12 10  6  . 2021 2013 2010 2009    . 1987
      12  .  .  .  . 2005    .    .    .    . 1983
      10  .  .  .  . 2010    .    .    .    . 1989
       4 10 11  5  9 2019 2016 2009 2007 2002 1982
       8  2  .  .  . 2016 2012    .    .    . 1992
       6  6 11  .  . 2017 2015 2012    .    . 1993
       7  7  7  .  . 2013 2008 2008    .    . 1985
       7  5  .  .  . 2012 2009    .    .    . 1990
       7  .  .  .  . 2018    .    .    .    . 1992
       5 11  .  .  . 2006 1999    .    .    . 1982
      12 12  .  .  . 2021 2019    .    .    . 1987
       8  .  .  .  . 2019    .    .    .    . 1997
       8  2  8  .  . 2014 2008 2006    .    . 1983
       .  .  .  .  .    .    .    .    .    . 2006
       3 11 11  .  . 2014 2010 1998    .    . 1981
       4  .  .  .  . 2019    .    .    .    . 1993
       6 11  .  .  . 2014 2007    .    .    . 1983
      10  8  3  .  . 2015 2014 2013    .    . 1989
      11  8  8  .  . 2013 2003 1996    .    . 1978
       5  .  .  .  . 2012    .    .    .    . 1990
       7  9  .  .  . 2013 2006    .    .    . 1984
       4  2  5  2  . 2020 2015 2013 2012    . 1995
      10 11 12  4  . 2017 2014 2013 2011    . 1984
       .  .  .  .  .    .    .    .    .    . 1997
       5  .  .  .  . 2016    .    .    .    . 1995
       .  .  .  .  .    .    .    .    .    . 1983
       .  .  .  .  .    .    .    .    .    . 1997
       6  .  .  .  . 2018    .    .    .    . 1989
       6 12  .  .  . 2019 2014    .    .    . 1995
      11  .  .  .  . 2018    .    .    .    . 1994
       9  .  .  .  . 2021    .    .    .    . 1994
       8  6  .  .  . 2015 2003    .    .    . 1989
       4  6  3  4  1 2022 2018 2016 2014 2013 1988
       .  .  .  .  .    .    .    .    .    . 2007
      12  6  8  .  . 2012 2009 2004    .    . 1984
       2  3  .  .  . 2020 2013    .    .    . 1992
       6  .  .  .  . 2003    .    .    .    . 1984
       1  .  .  .  . 2019    .    .    .    . 1995
       3  3  1  .  . 2014 2014 2007    .    . 1985
       7  5  9  6  . 2021 2017 2015 2006    . 1988
       9 12  6  .  . 2011 2008 2004    .    . 1986
       1  7  .  .  . 2022 2016    .    .    . 1995
       4  9  .  .  . 2018 2015    .    .    . 1986
       4  9  7  .  . 2017 2012 2008    .    . 1986
      12  4  4  .  . 2012 2010 2010    .    . 1991
       .  .  .  .  .    .    .    .    .    . 2001
      12  7  .  .  . 2006 1998    .    .    . 1975
       .  .  .  .  .    .    .    .    .    . 1998
       .  .  .  .  .    .    .    .    .    . 1994
       5  6  .  .  . 2008 2003    .    .    . 1972
      12 11  .  .  . 2018 2013    .    .    . 1989
       .  .  .  .  .    .    .    .    .    . 2003
       .  .  .  .  .    .    .    .    .    . 1997
       7 12  7 10  . 2020 2008 2005 2003    . 1986
       4 10  .  .  . 2020 2017    .    .    . 1997
       1  3  2 11  7 2017 2011 2009 2006 2003 1976
       7  4  .  .  . 2013 2012    .    .    . 1984
       5  5  .  .  . 2021 2020    .    .    . 1996
       1  4  4  9  . 2022 2017 2015 2012    . 1992
       6  .  .  .  . 2021    .    .    .    . 1998
      10 10  7  .  . 2015 2010 2006    .    . 1984
       .  .  .  .  .    .    .    .    .    . 2006
       7  7  4  .  . 2020 2020 2017    .    . 1994
       5  .  .  .  . 2018    .    .    .    . 1997
      10  4  .  .  . 2021 2018    .    .    . 1990
       4  .  .  .  . 2016    .    .    .    . 1992
       5  3  .  .  . 2017 2013    .    .    . 1993
       5  5  9  1  . 2019 2018 2014 2012    . 1989
       5  .  .  .  . 2021    .    .    .    . 1998
       6 12  .  .  . 2012 1997    .    .    . 1978
       2  .  .  .  . 2022    .    .    .    . 1998
       9  8  .  .  . 2012 2006    .    .    . 1982
       8  8  .  .  . 2016 2012    .    .    . 1994
       3  3  3  .  . 2019 2014 2009    .    . 1990
       1  .  .  .  . 2019    .    .    .    . 1996
       6  4  3  9  . 2016 2014 2012 2009    . 1992
       5  8  5  .  . 2021 2019 2013    .    . 1992
       4  .  .  .  . 2017    .    .    .    . 1994
       8  5 11 11  5 2008 2005 2003 1995 1994 1980
      10  2  3  4  8 2011 2007 2005 2003 2000 1979
       5  .  .  .  . 2022    .    .    .    . 2000
       .  .  .  .  .    .    .    .    .    . 2005
       5  4  .  .  . 2018 2016    .    .    . 1995
       2  8  8  6  . 2020 2008 1998 1996    . 1975
       .  .  .  .  .    .    .    .    .    . 1996
       2  .  .  .  . 2021    .    .    .    . 1995
      12  1  6  .  . 2019 2018 2016    .    . 1996
       1  2 11  .  . 2013 2012 2000    .    . 1981
       .  .  .  .  .    .    .    .    .    . 2000
      10 12 11  8  . 2017 2000 1996 1992    . 1974
      12 12 12  .  . 2018 2015 2015    .    . 1993
      12 10 10  6  . 2018 2017 2011 2008    . 1990
       9 12  8  .  . 2020 2016 2014    .    . 1996
       6  6  7  .  . 2013 2010 2006    .    . 1988
      10  6  6  9 12 2019 2013 2011 2009 2007 1987
      11  .  .  .  . 2002    .    .    .    . 1981
      11  .  .  .  . 2014    .    .    .    . 1997
       .  .  .  .  .    .    .    .    .    . 1996
      10  .  .  .  . 2020    .    .    .    . 1992
       3  8  .  .  . 2015 2005    .    .    . 1989
      end
      
      gen long respid=_n
      reshape long b1_ b2_, i(respid) j(which) string
      gen birthyear= ym(b2_, b1_)
      contract birthyear, nomiss
      keep if birthyear>= tm(2017m6)
      tw connected _freq birthyear, ms(Oh) xtitle("") ///
       xlab(`=ym(2017,7)' (6) `=ym(2022,7)', format(%tmYm)) ///
      xsc(r(. `=ym(2022,9)')) ytitle(Teenage Pregnancies)
      Click image for larger version

Name:	Graph.png
Views:	1
Size:	102.6 KB
ID:	1748876

      Comment


      • #18
        Thanks a lot! The above code worked independently but does not unfortunately relate to births (or pregnancies) for the years 2017 to 2022. The graph relates to the universe of births across all age groups. Sorry, I guess I didn't give as much information as was required.

        Essentially, since the DHS is cross-sectional and only takes the respondents' age-group at a point in time, I would like to structure my data to depict teenagers in 2017, 2018, 2019 all the way to 2022. I managed this (see attached a simple year by year count of teenage pregnancies).

        My challenge is isolating them on a monthly basis so that my twoway connected graph shows these monthly counts of births (also with specified intervals). As such, the count of births in July 2017 should only depict the births of teenagers at that point in time (births relating to respondents that were teenagers in July 2017). The same should be for births in, for example, March 2022.

        Please see below my commands. The contract command does not work and returns an error stating "no observations". This is where I am stuck and I am also wondering whether I am doing the right thing. I have also provided the essential variables (data) I use for my analysis after my keep command. I use the 15-19 age-group as the teenagers and this is coded as 1 in the dataset.

        Code:
        clear all
        
        cd "/Users/melynoluoch/Documents/MasterThesis/DHS2022/KEIR8BDT"
        
        capture log close
        log using MasterThesis, replace
        
        use KEIR8BFL.DTA, clear
        
        
        *YEARS FOR ANALYSIS: keep only years 2017 to 2022 for the variables relating to year and month of birth (b1_01 to b1_20, b2_01 to b2_20)
          foreach var of varlist b2_01-b2_20 b1_01-b1_09 {
         
         replace `var' = 0 if `var' < 2017 | `var' > 2022
        }
        
        
        /*keeping only variables required for analysis of 2017 to 2022 trend analysis of teenage births, v013 (respondent's age-group) v010 (respondent's year of birth), v025 (urban vs rural), v101 (region), v190(wealth), v106(highest educ level), v151(sex of hh head), v152(age of household head) */
        
        keep v013 b1_01-b1_09 b2_01-b2_09 v010 v025 v101 v190 v106 v151 v152
        
        
        *TRANSFORMING THE DATA for ease of analysis
        *Sort the dataset by the age-group variable
        sort v013
        
        *Creating a new identifier variable named "respid" i.e. respondent id
        gen long respid=_n
        
        save "Output/BeforeReshape", replace
        
        
        /*Creating teenager-dummy variables from years 2017-2022. v010 is the year of birth variable. For example, an individual woman is a teenager in 2017 (between age 15-19) if they were born between 1998 and 2002*/
        
        gen Teenage_2017=1 if v010 <=2002 & v010 >=1998
        gen Teenage_2018=1 if v010 <=2003 & v010 >=1999
        gen Teenage_2019=1 if v010 <=2004 & v010 >=2000
        gen Teenage_2020=1 if v010 <=2005 & v010 >=2001
        gen Teenage_2021=1 if v010 <=2006 & v010 >=2002
        gen Teenage_2022=1 if v010 <=2007 & v010 >=2003
        
        *creating new variables for years 2017 to 2022 with the prefix "year_"
        gen year_2017=0
        gen year_2018=0
        gen year_2019=0
        gen year_2020=0
        gen year_2021=0
        gen year_2022=0
        
        *creating observations (count) for children to teenagers from the "year of birth" variables b2_01 to b2_09
        foreach n of varlist b2_01-b2_09 {
        
        replace year_2017= year_2017 + (`n'==2017) if Teenage_2017==1
        replace year_2018= year_2018 + (`n'==2018) if Teenage_2018==1
        replace year_2019= year_2019 + (`n'==2019) if Teenage_2019==1
        replace year_2020= year_2020 + (`n'==2020) if Teenage_2020==1
        replace year_2021= year_2021 + (`n'==2021) if Teenage_2021==1
        replace year_2022= year_2022 + (`n'==2022) if Teenage_2022==1
            
        }
        
        * reshape the variables p1_ year_ from wide to long format, creating a new variable for outcome year
        reshape long b1_ b2_, i(respid) j(which) string
        
        save "Output/Reshaped", replace
        
        * convert year and month values into a monthly date format
        gen birthyear= ym(b2_, b1_)
        
        * determines the frequency of pregnancies in each month of a specific year
        contract birthyear, nomiss
        
        * keep only observations where the "birthyear" variable represents a date after or equal to Feb 2017
        keep if birthyear>= tm(2017m2)
        
        * graph a twoway connected line starting with March 2017 to March 2022
        tw connected _freq birthyear, ms(Oh) xtitle("") ///
         xlab(`=ym(2017,3)' (6) `=ym(2022,3)', format(%tmYm)) ///
        xsc(r(. `=ym(2022,3)')) ytitle(Teenage Pregnancies)
        
        graph export "Output/TwowayConnect_PregbyMonth.pdf", replace




        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input byte(v013 b1_01 b1_02 b1_03 b1_04 b1_05 b1_06 b1_07 b1_08 b1_09) int(b2_01 b2_02 b2_03 b2_04 b2_05 b2_06 b2_07 b2_08 b2_09 v010)
        4 0 0 0 0 0 0 0 0 0 2021    0    0 0 0 0 0 0 0 1987
        5 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1983
        4 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1989
        5 0 0 0 0 0 0 0 0 0 2019    0    0 0 0 0 0 0 0 1982
        4 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1992
        3 0 0 0 0 0 0 0 0 0 2017    0    0 0 0 0 0 0 0 1993
        5 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1985
        4 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1990
        4 0 0 0 0 0 0 0 0 0 2018    0    0 0 0 0 0 0 0 1992
        5 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1982
        4 0 0 0 0 0 0 0 0 0 2021 2019    0 0 0 0 0 0 0 1987
        2 0 0 0 0 0 0 0 0 0 2019    0    0 0 0 0 0 0 0 1997
        5 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1983
        1 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 2006
        6 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1981
        3 0 0 0 0 0 0 0 0 0 2019    0    0 0 0 0 0 0 0 1993
        5 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1983
        4 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1989
        6 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1978
        4 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1990
        5 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1984
        3 0 0 0 0 0 0 0 0 0 2020    0    0 0 0 0 0 0 0 1995
        5 0 0 0 0 0 0 0 0 0 2017    0    0 0 0 0 0 0 0 1984
        2 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1997
        3 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1995
        5 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1983
        2 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1997
        4 0 0 0 0 0 0 0 0 0 2018    0    0 0 0 0 0 0 0 1989
        3 0 0 0 0 0 0 0 0 0 2019    0    0 0 0 0 0 0 0 1995
        3 0 0 0 0 0 0 0 0 0 2018    0    0 0 0 0 0 0 0 1994
        3 0 0 0 0 0 0 0 0 0 2021    0    0 0 0 0 0 0 0 1994
        4 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1989
        4 0 0 0 0 0 0 0 0 0 2022 2018    0 0 0 0 0 0 0 1988
        1 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 2007
        5 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1984
        3 0 0 0 0 0 0 0 0 0 2020    0    0 0 0 0 0 0 0 1992
        5 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1984
        3 0 0 0 0 0 0 0 0 0 2019    0    0 0 0 0 0 0 0 1995
        5 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1985
        4 0 0 0 0 0 0 0 0 0 2021 2017    0 0 0 0 0 0 0 1988
        5 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1986
        3 0 0 0 0 0 0 0 0 0 2022    0    0 0 0 0 0 0 0 1995
        5 0 0 0 0 0 0 0 0 0 2018    0    0 0 0 0 0 0 0 1986
        5 0 0 0 0 0 0 0 0 0 2017    0    0 0 0 0 0 0 0 1986
        4 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1991
        2 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 2001
        7 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1975
        2 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1998
        3 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1994
        7 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1972
        4 0 0 0 0 0 0 0 0 0 2018    0    0 0 0 0 0 0 0 1989
        1 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 2003
        2 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1997
        5 0 0 0 0 0 0 0 0 0 2020    0    0 0 0 0 0 0 0 1986
        2 0 0 0 0 0 0 0 0 0 2020 2017    0 0 0 0 0 0 0 1997
        7 0 0 0 0 0 0 0 0 0 2017    0    0 0 0 0 0 0 0 1976
        5 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1984
        3 0 0 0 0 0 0 0 0 0 2021 2020    0 0 0 0 0 0 0 1996
        3 0 0 0 0 0 0 0 0 0 2022 2017    0 0 0 0 0 0 0 1992
        2 0 0 0 0 0 0 0 0 0 2021    0    0 0 0 0 0 0 0 1998
        5 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1984
        1 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 2006
        3 0 0 0 0 0 0 0 0 0 2020 2020 2017 0 0 0 0 0 0 1994
        2 0 0 0 0 0 0 0 0 0 2018    0    0 0 0 0 0 0 0 1997
        4 0 0 0 0 0 0 0 0 0 2021 2018    0 0 0 0 0 0 0 1990
        3 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1992
        3 0 0 0 0 0 0 0 0 0 2017    0    0 0 0 0 0 0 0 1993
        4 0 0 0 0 0 0 0 0 0 2019 2018    0 0 0 0 0 0 0 1989
        2 0 0 0 0 0 0 0 0 0 2021    0    0 0 0 0 0 0 0 1998
        6 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1978
        2 0 0 0 0 0 0 0 0 0 2022    0    0 0 0 0 0 0 0 1998
        5 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1982
        3 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1994
        4 0 0 0 0 0 0 0 0 0 2019    0    0 0 0 0 0 0 0 1990
        3 0 0 0 0 0 0 0 0 0 2019    0    0 0 0 0 0 0 0 1996
        3 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1992
        3 0 0 0 0 0 0 0 0 0 2021 2019    0 0 0 0 0 0 0 1992
        3 0 0 0 0 0 0 0 0 0 2017    0    0 0 0 0 0 0 0 1994
        6 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1980
        6 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1979
        2 0 0 0 0 0 0 0 0 0 2022    0    0 0 0 0 0 0 0 2000
        1 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 2005
        3 0 0 0 0 0 0 0 0 0 2018    0    0 0 0 0 0 0 0 1995
        7 0 0 0 0 0 0 0 0 0 2020    0    0 0 0 0 0 0 0 1975
        3 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1996
        3 0 0 0 0 0 0 0 0 0 2021    0    0 0 0 0 0 0 0 1995
        3 0 0 0 0 0 0 0 0 0 2019 2018    0 0 0 0 0 0 0 1996
        6 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1981
        2 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 2000
        7 0 0 0 0 0 0 0 0 0 2017    0    0 0 0 0 0 0 0 1974
        3 0 0 0 0 0 0 0 0 0 2018    0    0 0 0 0 0 0 0 1993
        4 0 0 0 0 0 0 0 0 0 2018 2017    0 0 0 0 0 0 0 1990
        3 0 0 0 0 0 0 0 0 0 2020    0    0 0 0 0 0 0 0 1996
        4 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1988
        5 0 0 0 0 0 0 0 0 0 2019    0    0 0 0 0 0 0 0 1987
        6 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1981
        2 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1997
        3 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1996
        3 0 0 0 0 0 0 0 0 0 2020    0    0 0 0 0 0 0 0 1992
        4 0 0 0 0 0 0 0 0 0    0    0    0 0 0 0 0 0 0 1989
        end
        label values v013 V013
        label def V013 1 "15-19", modify
        label def V013 2 "20-24", modify
        label def V013 3 "25-29", modify
        label def V013 4 "30-34", modify
        label def V013 5 "35-39", modify
        label def V013 6 "40-44", modify
        label def V013 7 "45-49", modify
        Attached Files

        Comment


        • #19
          You can just create an indicator for teenage mothers and then use only their observations.

          Code:
          gen long respid=_n
          reshape long b1_ b2_, i(respid) j(which) string
          gen birthyear= ym(b2_, b1_)
          gen teenager= inrange(b2_-v010, 13, 19)
          contract birthyear if teenager, nomiss
          keep if birthyear>= tm(2017m6)
          tw connected _freq birthyear, ms(Oh) xtitle("") ///
           xlab(`=ym(2017,7)' (6) `=ym(2022,7)', format(%tmYm)) ///
          xsc(r(. `=ym(2022,9)')) ytitle(Teenage Pregnancies)

          Comment


          • #20
            The suggested commands still return the same error at the contract command line

            Comment


            • #21
              There is a difference between the data example you provided in #16, on which my code is based, and the one in #18. Where did the birth months go? They are all 0s in #18. Nonetheless, I believe the advice provided here is sufficient for you to complete your task. Simply examine what my code is doing and ensure that your dataset follows the structure outlined in #16. My intention here is solely to provide guidance.

              Comment


              • #22
                Sorry. I've realised my error. I had restricted observations for years 2017 to 2022 only. It now worked when I removed this restriction. Thank you Andrew Musau!

                Comment


                • #23
                  Hello great people,

                  Kindly assist in this request.

                  My goal is to do an event study analysis for TeenageBirths pre- and post-pandemic similar to the one attached. I would like to fit a regression of teenage briths in each region/county (v101) at a specific time period using data from Jan 2017 to December 2019 as per the attached snipshot capturing equation 1. After the regression and using the estimated coefficient from equation 1, I'd like to predict the residuals then have the difference between the actual and predicted measure as the excess TeenageBirths and my main outcome of interest. The event study model regresses these excess TeenageBirths for county s in time t on a vector of month indicators, using data from January 2017 through Feb 2021 as per equation 2. With the below commands (displayed after the data), my regress command returns the error "no observations".

                  I'm using the reshaped data below:

                  Code:
                  * Example generated by -dataex-. For more info, type help dataex
                  clear
                  input long respid str2 which int v010 byte(v013 v101 b1_) int b2_
                   1 "01" 2005 1  8 . .
                   1 "02" 2005 1  8 . .
                   1 "03" 2005 1  8 . .
                   1 "04" 2005 1  8 . .
                   1 "05" 2005 1  8 . .
                   1 "06" 2005 1  8 . .
                   1 "07" 2005 1  8 . .
                   1 "08" 2005 1  8 . .
                   1 "09" 2005 1  8 . .
                   2 "01" 2005 1  5 . .
                   2 "02" 2005 1  5 . .
                   2 "03" 2005 1  5 . .
                   2 "04" 2005 1  5 . .
                   2 "05" 2005 1  5 . .
                   2 "06" 2005 1  5 . .
                   2 "07" 2005 1  5 . .
                   2 "08" 2005 1  5 . .
                   2 "09" 2005 1  5 . .
                   3 "01" 2004 1 40 . .
                   3 "02" 2004 1 40 . .
                   3 "03" 2004 1 40 . .
                   3 "04" 2004 1 40 . .
                   3 "05" 2004 1 40 . .
                   3 "06" 2004 1 40 . .
                   3 "07" 2004 1 40 . .
                   3 "08" 2004 1 40 . .
                   3 "09" 2004 1 40 . .
                   4 "01" 2002 1 36 . .
                   4 "02" 2002 1 36 . .
                   4 "03" 2002 1 36 . .
                   4 "04" 2002 1 36 . .
                   4 "05" 2002 1 36 . .
                   4 "06" 2002 1 36 . .
                   4 "07" 2002 1 36 . .
                   4 "08" 2002 1 36 . .
                   4 "09" 2002 1 36 . .
                   5 "01" 2005 1 12 . .
                   5 "02" 2005 1 12 . .
                   5 "03" 2005 1 12 . .
                   5 "04" 2005 1 12 . .
                   5 "05" 2005 1 12 . .
                   5 "06" 2005 1 12 . .
                   5 "07" 2005 1 12 . .
                   5 "08" 2005 1 12 . .
                   5 "09" 2005 1 12 . .
                   6 "01" 2004 1 37 . .
                   6 "02" 2004 1 37 . .
                   6 "03" 2004 1 37 . .
                   6 "04" 2004 1 37 . .
                   6 "05" 2004 1 37 . .
                   6 "06" 2004 1 37 . .
                   6 "07" 2004 1 37 . .
                   6 "08" 2004 1 37 . .
                   6 "09" 2004 1 37 . .
                   7 "01" 2006 1 44 . .
                   7 "02" 2006 1 44 . .
                   7 "03" 2006 1 44 . .
                   7 "04" 2006 1 44 . .
                   7 "05" 2006 1 44 . .
                   7 "06" 2006 1 44 . .
                   7 "07" 2006 1 44 . .
                   7 "08" 2006 1 44 . .
                   7 "09" 2006 1 44 . .
                   8 "01" 2005 1 18 . .
                   8 "02" 2005 1 18 . .
                   8 "03" 2005 1 18 . .
                   8 "04" 2005 1 18 . .
                   8 "05" 2005 1 18 . .
                   8 "06" 2005 1 18 . .
                   8 "07" 2005 1 18 . .
                   8 "08" 2005 1 18 . .
                   8 "09" 2005 1 18 . .
                   9 "01" 2002 1 47 . .
                   9 "02" 2002 1 47 . .
                   9 "03" 2002 1 47 . .
                   9 "04" 2002 1 47 . .
                   9 "05" 2002 1 47 . .
                   9 "06" 2002 1 47 . .
                   9 "07" 2002 1 47 . .
                   9 "08" 2002 1 47 . .
                   9 "09" 2002 1 47 . .
                  10 "01" 2005 1 37 . .
                  10 "02" 2005 1 37 . .
                  10 "03" 2005 1 37 . .
                  10 "04" 2005 1 37 . .
                  10 "05" 2005 1 37 . .
                  10 "06" 2005 1 37 . .
                  10 "07" 2005 1 37 . .
                  10 "08" 2005 1 37 . .
                  10 "09" 2005 1 37 . .
                  11 "01" 2004 1 39 . .
                  11 "02" 2004 1 39 . .
                  11 "03" 2004 1 39 . .
                  11 "04" 2004 1 39 . .
                  11 "05" 2004 1 39 . .
                  11 "06" 2004 1 39 . .
                  11 "07" 2004 1 39 . .
                  11 "08" 2004 1 39 . .
                  11 "09" 2004 1 39 . .
                  12 "01" 2006 1 27 . .
                  end
                  label values v013 V013
                  label def V013 1 "15-19", modify
                  label values v101 V101
                  label def V101 5 "lamu", modify
                  label def V101 8 "wajir", modify
                  label def V101 12 "meru", modify
                  label def V101 18 "nyandarua", modify
                  label def V101 27 "uasin gishu", modify
                  label def V101 36 "bomet", modify
                  label def V101 37 "kakamega", modify
                  label def V101 39 "bungoma", modify
                  label def V101 40 "busia", modify
                  label def V101 44 "migori", modify
                  label def V101 47 "nairobi", modify


                  Code:
                  use "Output/Reshaped", clear
                      gen birthyear = ym(b2_, b1_)
                      gen teenager = inrange(b2_ - v010, 15, 19)
                  
                      * Change format of birth year variable
                      gen birthyear_C = birthyear
                      format %tm birthyear_C 
                      
                      * Creates a binary variable pre_sample, taking the value 1 if the birth year falls within the range from 2017 to 2019, and 0 otherwise.
                      gen pre_sample = (birthyear_C >= date("2017m1","YM")) &  (birthyear_C <= date("2019m12","YM"))
                      
                      * Taking 2016 as the base year because the analysis begins with year 2017. By subtracting 2016 from each year, I intend to set 2017 as year 1 in the event study analysis
                      replace b2_ = b2_ - 2016
                          
                      contract birthyear_C pre_sample b2_ b1_ v101 respid if teenager, nomiss
                  
                      rename _freq TeenageBirths
                      
                      reg TeenageBirths b2_ b1_ if pre_sample
                  Attached Files

                  Comment

                  Working...
                  X