Announcement

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

  • from (year) to (year) into single year observations

    Hi there,

    first off, I spend quite some time searching for an answer for my (I guess) rather basic question but couldn't find something fitting. I'm currently writing my bachelor thesis and I am grateful for any help.

    I'm trying to get the Ethnic Power Relations (EPR 2014) dataset into a form I can merge it with my master dataset. My problem is that there are multiple observations for every ethnic group per country, but only over a certain time period. My master-set variables range from 1988 to 2012 so I already adjusted the periods. I dropped some variables to fit everything of interest into one screenshot so don't be confused. The multiple observations per country/period actually contain the respective ethnic groups.

    Click image for larger version

Name:	stata_question.PNG
Views:	1
Size:	24.5 KB
ID:	1439716


    My question is how to generate new observations for every year between "from" and "to". The "powersize" etc. variables only change with the periods, not yearly. Therefore the new observations should include the values for the respective "from"-"to" period.

    If there is no way to do so, is there any form of the "merge" command that matches every e.g. 1988-2008 observations to every fitting observation in the master dataset?

    Thanks in advance and best regards

    Dorian


  • #2
    Code:
    gen long obs_no = _n
    expand to - from + 1
    by obs_no, sort: gen year == from + _n - 1
    should be about right.

    This is not tested because your example data was given as a screenshot which, while it gives the a visual impression of the data, is cannot be worked with. In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 15.1 or a fully updated version 14.2, it is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment


    • #3
      This was exactly what I needed, thank you very much! I'll look up the -dataex- command and use it in the future.

      Thanks for the quick and detailed answer!

      Comment

      Working...
      X