Announcement

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

  • Subtract over a particular pair of months

    I have a problem with my panel data ( which is organised by month and municipality) were I want to take the difference between a pair of month from two different years, ex the observation for jan 2020 would be jan2020-jan2019 and the year before would imply jan2019-jan2018. This will be done for all month in my sample starting from May 2018 and going on to May 2020. I have played around with "egen" but don't really see how I can solve it. Any ideas how I could fix it?

  • #2
    As you have not supplied example data, I cannot give you specific code because the details depend on things left to the imagination. But here's the general approach:

    1. You need to have a true Stata internal format monthly date variable for this. If your current date variable is not that, you must convert it. See -help datetime functions- if you don't know how to do that. Exactly how to do it depends on what your date variable currently is.

    2. Run the -xtset- command with your municipality as the panel variable and the monthly date variable as the time variable.

    3. If you want this difference for a variable named X, the code is now -gen X_diff = X - L12.X if inrange(month, tm(2018m5), tm(2020m5)-.

    If you need more specific help, use the -dataex- command to post back with example data. If you are running version 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.

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

    Comment


    • #3
      Thanks for your reply Clyde, it worked perfectly! I will have the dataex command in mind next time.

      Comment

      Working...
      X