Announcement

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

  • how to add values for 3 years for each observation

    Hi my data set looks like this
    gvkey fyear liva n liva_2 liva_3 liva_5
    1004 1999 -0.81841 1
    1004 2001 -0.12328 2 -0.94168
    1004 2004 -0.29572 3 -0.41899 -1.2374
    1004 2005 0.547 4
    1004 2006 -0.0125 5 15
    1004 2008 -0.26287 6
    1004 2010 0.059042 7
    1004 2011 -0.35232 8
    1004 2013 0.271471 9
    1004 2014 -0.06726 10
    1004 2015 -0.06743 11
    1004 2016 0.211439 12
    1004 2017 -0.03207 13
    1004 2018 0.107273 14
    1013 1999 8.703646 1
    1013 2001 -26.258 2
    1013 2002 -5.24997 3
    1013 2003 0.170867 4
    1013 2004 -1.53502 5
    1013 2005 0.201077 6
    1013 2006 -2.42768 7
    1013 2008 -0.94143 8
    1013 2009 -0.24918 9
    1034 1999 -0.64264 1
    1034 2000 0.686527 2
    1034 2001 -1.01673 3
    1034 2002 -1.24141 4
    1034 2003 0.404838 5
    1034 2005 0.769777 6
    1034 2006 -0.72259 7
    1034 2007 -0.49277 8
    I want to generate liva_2, liva_3 and liva_5 and as the the name goes, for liva_2 for each gvkey i want to add the current year and prior year liva.
    for liva_3 I will need to add current year and last 2 years liva ( for each gvkey)
    for liva_5 I will have to add current year nd last 4 years liva.

    please help me with the code.

  • #2
    Code:
    xtset gvkey year
    *CURRENT YEAR + 1 YEAR LAG
    gen wanted1= liva+ L.liva
    
    *CURRENT YEAR + 1 YEAR LAG + 2 YEAR LAG + 3 YEAR LAG
    gen wanted2= liva+ L.liva + L2.liva + L3.liva

    For an extended lag period, see a trick in the linked thread by Nick Cox attributed to Michael Blasnik.
    https://www.stata.com/statalist/arch.../msg00278.html
    Last edited by Andrew Musau; 24 May 2022, 04:13.

    Comment


    • #3
      The data example shows many gaps which are likely to confuse if not handled directly. I would tend to use rangestat from SSC in more or less this way:


      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input int(gvkey fyear) float liva
      1004 1999  -.81841
      1004 2001  -.12328
      1004 2004  -.29572
      1004 2005     .547
      1004 2006   -.0125
      1004 2008  -.26287
      1004 2010  .059042
      1004 2011  -.35232
      1004 2013  .271471
      1004 2014  -.06726
      1004 2015  -.06743
      1004 2016  .211439
      1004 2017  -.03207
      1004 2018  .107273
      1013 1999 8.703646
      1013 2001  -26.258
      1013 2002 -5.24997
      1013 2003  .170867
      1013 2004 -1.53502
      1013 2005  .201077
      1013 2006 -2.42768
      1013 2008  -.94143
      1013 2009  -.24918
      1034 1999  -.64264
      1034 2000  .686527
      1034 2001 -1.01673
      1034 2002 -1.24141
      1034 2003  .404838
      1034 2005  .769777
      1034 2006  -.72259
      1034 2007  -.49277
      end
      
      rangestat (count) count3=liva (sum) sum3=liva, int(fyear -2 0) by(gvkey)
      
      list, sepby(gvkey)
      
          +------------------------------------------------+
           | gvkey   fyear       liva   count3         sum3 |
           |------------------------------------------------|
        1. |  1004    1999    -.81841        1   -.81840998 |
        2. |  1004    2001    -.12328        2   -.94168998 |
        3. |  1004    2004    -.29572        1   -.29572001 |
        4. |  1004    2005       .547        2    .25127998 |
        5. |  1004    2006     -.0125        3    .23877998 |
        6. |  1004    2008    -.26287        2   -.27537001 |
        7. |  1004    2010    .059042        2   -.20382801 |
        8. |  1004    2011    -.35232        2   -.29327799 |
        9. |  1004    2013    .271471        2   -.08084899 |
       10. |  1004    2014    -.06726        2      .204211 |
       11. |  1004    2015    -.06743        3      .136781 |
       12. |  1004    2016    .211439        3      .076749 |
       13. |  1004    2017    -.03207        3      .111939 |
       14. |  1004    2018    .107273        3      .286642 |
           |------------------------------------------------|
       15. |  1013    1999   8.703646        1    8.7036457 |
       16. |  1013    2001    -26.258        2   -17.554354 |
       17. |  1013    2002   -5.24997        2   -31.507969 |
       18. |  1013    2003    .170867        3   -31.337102 |
       19. |  1013    2004   -1.53502        3    -6.614123 |
       20. |  1013    2005    .201077        3    -1.163076 |
       21. |  1013    2006   -2.42768        3    -3.761623 |
       22. |  1013    2008    -.94143        2     -3.36911 |
       23. |  1013    2009    -.24918        2     -1.19061 |
           |------------------------------------------------|
       24. |  1034    1999    -.64264        1   -.64263999 |
       25. |  1034    2000    .686527        2    .04388702 |
       26. |  1034    2001   -1.01673        3   -.97284293 |
       27. |  1034    2002   -1.24141        3    -1.571613 |
       28. |  1034    2003    .404838        3    -1.853302 |
       29. |  1034    2005    .769777        2     1.174615 |
       30. |  1034    2006    -.72259        2    .04718697 |
       31. |  1034    2007    -.49277        3   -.44558302 |
           +------------------------------------------------+
      Given so many gaps, it's a decision for the researcher whether means work better than sums.

      Different lags require different runs.

      Comment

      Working...
      X