Announcement

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

  • Add new years to Religion Datafile

    Hello,
    I am a new Stata user. The following question: I have memorized a thearda religion dataset from 2000 and 2010. After that I converted it to long format. My problem is that I would like to interpolate the years between 2000 and 2010. However, these years are not yet in the dataset at all. How can I insert new years for which there is no value in Stata.

  • #2
    Code:
    . // create some example data
    . clear
    
    . input id year x
    
                id       year          x
      1. 1 2000 2
      2. 1 2010 4
      3. 2 2000 3
      4. 2 2010 8
      5. end
    
    .
    . // for each id make new 8 copies of the 2000 observation
    . // so the new data contains 9 copies of that observation, the original + the 8 new copies
    . expand 9 if year == 2000, gen(new)
    (16 observations created)
    
    .
    . // fix year
    . bysort id (year) : replace year = year + _n if new == 1
    (16 real changes made)
    
    .
    . // fix any other variables
    . replace x = . if new == 1
    (16 real changes made, 16 to missing)
    
    .
    . // we no longer need the variable new
    . drop new
    
    .
    . // admire the result
    . sort id year
    
    . list , sepby(id)
    
         +---------------+
         | id   year   x |
         |---------------|
      1. |  1   2000   2 |
      2. |  1   2002   . |
      3. |  1   2003   . |
      4. |  1   2004   . |
      5. |  1   2005   . |
      6. |  1   2006   . |
      7. |  1   2007   . |
      8. |  1   2008   . |
      9. |  1   2009   . |
     10. |  1   2010   4 |
         |---------------|
     11. |  2   2000   3 |
     12. |  2   2001   . |
     13. |  2   2002   . |
     14. |  2   2003   . |
     15. |  2   2004   . |
     16. |  2   2005   . |
     17. |  2   2007   . |
     18. |  2   2008   . |
     19. |  2   2009   . |
     20. |  2   2010   8 |
         +---------------+
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Thank you!

      Comment

      Working...
      X