Announcement

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

  • generating variable

    I have panel data and need to generate a new variable by taking the difference of variable "rent" for obs with variable "location" == "x" and obs with variable "location" == "y" if variable "time period" is the same. For example:
    Obs 1 has time period = 2023, rent = 12 and location = x. I would like to generate a new variable for obs 1 by subtracting (rent for location "x" minus rent for location "y" from obs 2 (12-10)
    Obs 2 has time period = 2023, rent = 10, and location =y .

    Any help would be appreciated.

  • #2
    Without a clearer picture of your data organization it is hard to answer this question. Is the observation containing location y in a given year always the one that immediately follows the one with location x in that year? Or is that just true for observations 1 and 2, and in the rest of the data set, other contingencies may apply? And are x and y the only two locations in the data set?

    Rather than answering those questions, it would be better for you to post back showing example data. The best way to do that is with the -dataex- command. 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.

    There is also the question of the generality of your goal here. Assuming there are other locations than x and y in the data set, are you only interested in the x - y difference, or are some other paired differences also going to be wanted? If so, which ones?

    Comment

    Working...
    X