Announcement

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

  • Question: how to compute a variable value based on its last value in a panel dataset

    Dear All,

    I am trying to do the calculation illustrated in Excel with Stata. Specifically, I would like to compute values in column M. But as you could see in the formula line, each value in column M needs the previous value as input to compute. For example, the value of M6 depends on the value of M5, and so on. I am struggling to achieve this using Stata with the codes below. It always give me missing values. I really appreciate it if someone could help me with this.


    Many thanks in advance for your assistance,
    Di

    Code:
         *generate counterfactuals (M column) 
        gen cf1 = 0 
        replace cf1 = a0 + a1*cf1'[_n-1] + b2*shock2 + b3* shock3 + et


    Click image for larger version

Name:	stata_q.PNG
Views:	1
Size:	56.5 KB
ID:	1633195

  • #2
    Di:
    I do hope that the follows toy-example helps:
    Code:
    . set obs 10
    number of observations (_N) was 0, now 10
    
    . g A=runiform()
    
    . g B=runiform()
    
    . g C=runiform()
    
    . g D= A+ (B* C[_n-1])
    
    . list
    
         +-------------------------------------------+
         |        A          B          C          D |
         |-------------------------------------------|
      1. | .3488717   .2047095   .9319346          . |
      2. | .2668857   .8927587   .4548882   1.098878 |
      3. | .1366463   .5844658   .0674011   .4025129 |
      4. | .0285569   .3697791   .3379889   .0534804 |
      5. | .8689333   .8506309   .9748848   1.156437 |
         |-------------------------------------------|
      6. | .3508549   .3913819   .7264384   .7324072 |
      7. | .0711051   .1196613   .0454151   .1580317 |
      8. |  .323368   .7542434   .7459667    .357622 |
      9. | .5551032   .6950234   .4961259   1.073567 |
     10. |  .875991   .6866152   .7167162   1.216639 |
         +-------------------------------------------+
    
    .
    Kind regards,
    Carlo
    (Stata 18.0 SE)

    Comment


    • #3
      Hi Carlo,

      Many thanks for your reply.

      Do you know what I shall do if I want to compute D= A+ (B* D[_n-1]) with D = 0 for the first observation? The key difficulty I am facing is that I need to generate a new variable and the value of this variable in time t depends not only on other variables at given time t but its previous value in t-1 as well. I don't know how to incorporate the latter part in the calculation.

      Thank you,
      Di

      Comment


      • #4
        Di:
        you can't.
        See the following toy-example that hopefully mimicks what you're after:
        Code:
        . set obs 1
        number of observations (_N) was 0, now 1
        
        . g D=0
        
        . g A=1
        
        . g B=A+D[_n-1]
        (1 missing value generated)
        
        . list
        
             +-----------+
             | D   A   B |
             |-----------|
          1. | 0   1   . |
             +-----------+
        
        .
        Kind regards,
        Carlo
        (Stata 18.0 SE)

        Comment

        Working...
        X