Announcement

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

  • Filling in Missing Years

    Hi Statalist,

    I'm trying to fill in missing years for my dataset, but something is off. The original dataset looks like the following (small cut):
    Code:
    input int year float ccode2 byte type_of_violence
    2001   2 3
    2015   2 3
    2000  20 2
    2001  20 2
    2004  41 3
    2005  41 3
    2001  51 2
    2002  51 2
    2003  51 2
    2002  70 3
    2004  70 3
    2005  70 2
    2006  70 2
    2007  70 3
    2008  70 3
    2009  70 3
    2010  70 3
    2011  70 3
    2012  70 3
    2013  70 3
    2014  70 3
    2015  70 3
    2016  70 3
    I'm trying to "fill in" the missing years. What I need are years 2000-2016 for every "ccode2." For example, for ccode2, I need years 2000-2016 for the observations and so on. The type_of_violence will be "." for the missing years (which I will recode later). I'm trying to merge this dataset to the master dataset later. When I try to add the missing years using the following code:
    Code:
    use IV_Violent_Events, clear
    keep type_of_violence ccode2
    expand 17
    bysort type_of_violence ccode2:gen year=1999+_n
    merge m:1 type_of_violence ccode2 year using IV_Violent_Events.dta
    Some of the observations are okay, but some of them are pretty off.
    Code:
    130 1 2000 1
    130 1 2001 1
    130 1 2002 1
    130 1 2003 1
    130 1 2004 1
    130 1 2005 1
    130 1 2006 1
    130 1 2007 1
    130 1 2008 3
    130 1 2009 1
    130 1 2010 1
    130 1 2011 1
    130 1 2012 1
    130 1 2013 1
    130 1 2014 1
    130 1 2015 1
    130 1 2016 1
    135 1 2000 3
    135 1 2001 3
    135 1 2002 1
    135 1 2003 3
    135 1 2004 3
    135 1 2005 1
    135 1 2006 1
    135 1 2007 3
    135 1 2008 3
    135 1 2009 1
    135 1 2010 1
    135 1 2011 1
    135 1 2012 1
    135 1 2013 3
    135 1 2014 1
    135 1 2015 3
    135 1 2016 1
    135 1 2017 1
    135 1 2018 1
    135 1 2019 1
    135 1 2020 1
    135 1 2021 1
    135 1 2022 1
    135 1 2023 1
    135 1 2024 1
    135 1 2025 1
    135 1 2026 1
    135 1 2027 1
    135 1 2028 1
    135 1 2029 1
    135 1 2030 1
    135 1 2031 1
    I'm not entirely sure what the problem is with the code, or with anything else. Thank you in advance for any help, as usual.

    Best,
    MW

  • #2
    I don't know where that merge syntax came from. Following your data example (thanks)

    Code:
    fillin year ccode2
    is what I would do next.

    Comment


    • #3
      Originally posted by Nick Cox View Post
      I don't know where that merge syntax came from. Following your data example (thanks)

      Code:
      fillin year ccode2
      is what I would do next.
      Nick, thank you!

      I'm not entirely sure why merge is used. I got the code from a previous post, and I re-read it but not sure why it's used anymore. (As a reference, or in case anyone is wondering, it's here: https://www.statalist.org/forums/for...the-panel-data)

      Edited (forgot to ask a question): does the fillin command here work by filling in the missing values by reading 2000 as the min and 2016 as the max; thus, filling everything in from equal or greater than 2000 and everything equal or less than 2016?

      Best,
      MW
      Last edited by Monica White; 14 Feb 2019, 15:56. Reason: Forgot to ask the question below

      Comment


      • #4
        No. If the help isn't clear on this, then it's shown by experiment.

        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input float(var1 var2)
        2008 1
        2018 0
        end
        
        fillin var1 var2
        
        list
        
             +-----------------------+
             | var1   var2   _fillin |
             |-----------------------|
          1. | 2008      0         1 |
          2. | 2008      1         0 |
          3. | 2018      0         0 |
          4. | 2018      1         1 |
             +-----------------------+
        You need tsfill too.

        Comment

        Working...
        X