Announcement

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

  • Temporal decomposition - Function using data from 2 different rows of a table

    I want to do a temporal decomposition using STATA. Assume the following data below where x= y*z
    year x y z
    2015 8 4 2
    2016 12 3 4
    2017 16 4 4
    It is not necessary to show the entire decomposition but a crucial step is calculating the changes in the variables:
    year x y z delta x delta y delta
    2015 8 4 2
    2016 12 3 4 4 -1 2
    2017 16 4 4 4 1 0
    But this involves subtracting one row from the previous row . However, nearly all the STATA functions refer to expression within a row. How can I create a loop which calculates and generates the variables delta x,y,z?

    Thanks!

    Rutger




  • #2
    Hi Rutget
    Im going to assume that you ONLY have time series data. If that is the case, the following should do:
    Code:
    tsset year
    gen dx=d.x
    gen dy=d.y
    gen dz=d.z
    You can also see more option for time series operations at "https://www.stata.com/manuals/u11.pdf#u11.4.4"
    HTH
    Fernando

    Comment


    • #3
      Thanks Fernando!

      It is in fact a decomposition for 140 countries in the world. i.e. the data has the structure below.
      countrycode year x y z
      FRA 2015 8 4 2
      FRA 2016 12 3 4
      FRA 2017 16 4 4
      USA 2015 100 10 10
      USA 2016 121 11 11
      USA 2017 150 15 10
      etc, etc

      What would be the best way to loop over the countries and then do the years?

      Kind regards,

      Rutger

      Comment


      • #4
        Well, that is a slightly different problem but with the same solution.
        You need to set up the data as panel. So instead of using tsset, you need xtset countrycode year.
        This assumes that countrycode is numeric.
        HTH

        Comment


        • #5
          Thanks, worked great!

          Comment

          Working...
          X