Announcement

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

  • Generate Difference with D.

    hello,

    I'm working with panel data/time-series data.
    I tried to calculate the change one one variable from one year to another.
    I used the following code:
    .tsset
    panel variable: EntityName_n (unbalanced)
    time variable: Year, 1986 to 2012, but with gaps
    delta: 1 year
    .generate ShortTermDebtChange = D.RatioShortDebtTotalAssets
    and for another try I used the following Code:
    .sort EntityName_n Year
    .gen chgShortDebtnew = RatioShortDebtTotalAssets[Year]-RatioShortDebtTotalAssets[Year-1] if EntityName_n[Year]==EntityName_n[Year-1]

    Can somebody explain what's the difference between both approaches since I get different results.
    Thanks in advance

  • #2
    Sofie,
    Please write the Stata code between code tags, it makes it easier for us to see what you typed and what Stata told you.
    As usual we would recomand you to stay with the Stata command D., as manual operation often leads to mistakes. (Unfortunatly you're much likely to make mistakes than Stata is)

    In you specific case, it is probably because your panel is not balanced (i.e. Stata indicates "time variable: Year, 1986 to 2012, but with gaps")
    Then, the manual code probably doesn't respect one year lag, and compares two following observations.

    Actually I tried with a similar code than yours on a example sample, and I just get missing values.. Is this what you have?

    Anyway, the better hand-made code will lead to exact similarity with the lag command D. ; so you should just trust Stata.

    Comment


    • #3
      thanks a lot.
      I know that my data are unbalanced, so that's probably the mistake.
      so i will stay with the command D.

      Comment


      • #4
        Apart from unbalanced data, there is another issue here. The use of [Year] for subscripting is incorrect, though syntactically legal, so that your variable chgShortDebtnew is mostly garbage. You intended RatioShortDebtTotalAssets[Year] to mean the value of RatioShortDebtTotalAssets found in the observation where some time variable takes on the value Year, I'm guessing. But that isn't what it means to Stata. The notation var[X] means the value of var found in the X'th observation in the data set (in its current sort order). So, for example RatioShortDebtTotalAssets[2014] will not be understood as the ratio in the observation where year = 2014. It will mean the value of ratio in the 2014th observation in the data set. (And if your data doesn't have 2014 observations, then it will simply be a missing value.)

        Moreover, in your particular situation, Year is already a variable in your data set. Its occurrence in a place where a constant is expected (the subscript position), causes Stata to assume you mean the value of Year in the first observation--and this will be applied to every observation.

        You probably should re-read the manual sections about subscripting in Stata before proceeding with further work with this kind of data. Subscripting comes up a lot when working with time series, and you are at risk of making some really serious mistakes until you understand and can use this notation correctly.
        Last edited by Clyde Schechter; 31 Mar 2015, 08:41.

        Comment

        Working...
        X