Announcement

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

  • How to create a new year in a year variable AND how to use data from one year for another year?

    I'm quite new to Stata and struggling with what may be a simple task.

    I have a dataset which measures religion every 5 years. I'm in the process of merging this religion dataset with another dataset which measures other variables every single year.

    In my religion data set religion is measured in 1975, 1980, 1985 etc.
    In my other dataset, the year variable begins in 1979

    I'd like to know how I can create a year 1979 in my religion dataset and then how to use the observations from 1975 for the new 1979 observation.

    Thank you!

  • #2
    Something like
    Code:
    use religion_dataset, clear
    preserve
    quietly keep if year == 1975
    quietly replace year = 1979
    tempfile tempfil0
    quietly save `tmpfil0'
    restore
    append using `tmpfil0'
    You might want to consider instead just starting your analysis from 1980, when you have coeval data.

    Comment


    • #3
      Alas Joseph Coveney's solution works perfectly, there are quicker (in terms of runtime) ways to achieve the same result, not involving saving a file to disk an re-loading it afterwards. You could, for instance, duplicate every observation that fits the condition year==1975, and replace the year in the newly created duplicates with 1979. Consider the following code that performs this procedure:
      Code:
      tempvar marker
      use "religion_dataset" , clear
      expand 2 if (year == 1975) , generate(`marker')
      replace year = 1979 if (`marker')
      Again, this should produce the exact same result as Joseph's solution, but will be executed faster on a large dataset; for small datasets, you probably won't perceive the difference. Note that both solutions add the new observations (year==1979) at the very bottom of the dataset, so you might want to sort afterwards.

      Regards
      Bela

      PS: Just to avoid confusion -- the parentheses around the if-expressions in my code are optional; I tend to include them for better readability only.
      Last edited by Daniel Bela; 29 Jun 2018, 02:39. Reason: added PS

      Comment


      • #4
        Thank you both for your advice!

        Comment

        Working...
        X