Announcement

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

  • How to Recenter data

    I am working on a panel data with 31 provinces. Each with temperature data of year 1995, 2000, 2005, 2010 and 2015. Now I would like to adjust the temperatures according to the temperature in year 1995 for each province: Temp 1995 - Temp 1995 (=0), Temp 2000 - Temp 1995, Temp 2005 - Temp 1995 and so on.
    I tried to do this by gen temp_1995=temp if year==1995 and then gen recentredtemp = temp - temp_1995. However, it turns out temp_1995 takes on missing value for year = 2000 and after. I want the temp_1995 = the temperature in 1995 even for year = 2000, 2005, 2010 and 2015. How can I do that?
    Last edited by Kehan Yan; 18 Jan 2024, 16:40.

  • #2
    It sounds like you have a disfunctionally organized data set. But I cannot tell from your description what it looks like, so I can't offer you a solution on the information given.

    Please post back showing example data from your data set using 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.

    Comment


    • #3
      See https://journals.sagepub.com/doi/pdf...867X1101100210 for some technique.

      Code:
      bysort province : egen temp_1995 = total((year == 1995) * temp)
      may help if and only if your data are in standard panel layout.

      Comment


      • #4
        Clyde Schechter Thank you for your reply! This is what my data look like
        Click image for larger version

Name:	Screenshot 2024-01-19 at 09.29.50.png
Views:	1
Size:	224.4 KB
ID:	1740397
        The integers in the first column stand for provinces, the second column is the year, and the third is the temperature of each year for every province.
        Nick Cox Thank you for your reply! The code works and it is exactly what i am looking for.

        Comment


        • #5
          Good that your problem is resolved. In future questions, please don't post dataex output as an image; copy and paste it as CODE. FAQ Advice #12 explains.

          Comment

          Working...
          X