Announcement

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

  • panel data save new variable to new data file

    Hi guys! I have panel data and would like to create a new variable for change over time. For each district that I have data on, I want to create a new variable for change from 1990-2020. So I want to take the difference from 1990 to 2020 and save it in a new excel or state data file. Does anyone have suggestions on how to do this?

  • #2
    Something like this:
    Code:
    by panel (year), sort: egen value1990 = max(cond(year == 1990, myvariable, .))
    by panel (year): egen value 2020 = max(cond(year == 2020, myvariable, .))
    gen wanted = value2020-value1990
    Evidently you will need to use the actual names of the variables for your panel identifier, year, and variable whose difference you want to calculate.

    Now, as you did not provide example data, this code is based on what I imagine your data to look like. But if your data is not organized the way I imagine it to be, this code will fail to run at all or will produce incorrect results. So, if this doesn't solve your problem, when you post back, be sure to show example data, and be sure to use the -dataex- command to do that. If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- 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

    Working...
    X