Announcement

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

  • Lag of variable with condition of not being misssing

    I want to create the lag of a variable with one caveat. If it is missing, I want to use the previous period's value etc. So it is the most frequent non-missing lag. Any ideas?

  • #2
    This is just the usual procedure plus replacement of missing values by previous non-missing values as a standard FAQ.

    https://www.stata.com/support/faqs/d...issing-values/


    The "etc." implies to me that you want the previous non-missing value even if some while back.


    Code:
    . webuse grunfeld, clear 
    
    . tsset company year 
    
    Panel variable: company (strongly balanced)
     Time variable: year, 1935 to 1954
             Delta: 1 year
    
    . replace invest = . if year == 1937
    (10 real changes made, 10 to missing)
    
    . gen prev_invest = L.invest 
    (20 missing values generated)
    
    . list year *invest if company == 1 
    
         +--------------------------+
         | year   invest   prev_i~t |
         |--------------------------|
      1. | 1935    317.6          . |
      2. | 1936    391.8      317.6 |
      3. | 1937        .      391.8 |
      4. | 1938    257.7          . |
      5. | 1939    330.8      257.7 |
         |--------------------------|
      6. | 1940    461.2      330.8 |
      7. | 1941      512      461.2 |
      8. | 1942      448        512 |
      9. | 1943    499.6        448 |
     10. | 1944    547.5      499.6 |
         |--------------------------|
     11. | 1945    561.2      547.5 |
     12. | 1946    688.1      561.2 |
     13. | 1947    568.9      688.1 |
     14. | 1948    529.2      568.9 |
     15. | 1949    555.1      529.2 |
         |--------------------------|
     16. | 1950    642.9      555.1 |
     17. | 1951    755.9      642.9 |
     18. | 1952    891.2      755.9 |
     19. | 1953   1304.4      891.2 |
     20. | 1954   1486.7     1304.4 |
         +--------------------------+
    
    . bysort company (year) : replace prev_invest = prev_invest[_n-1] if missing(prev_invest)
    (10 real changes made)
    
    . list year *invest if company == 1 
    
         +--------------------------+
         | year   invest   prev_i~t |
         |--------------------------|
      1. | 1935    317.6          . |
      2. | 1936    391.8      317.6 |
      3. | 1937        .      391.8 |
      4. | 1938    257.7      391.8 |
      5. | 1939    330.8      257.7 |
         |--------------------------|
      6. | 1940    461.2      330.8 |
      7. | 1941      512      461.2 |
      8. | 1942      448        512 |
      9. | 1943    499.6        448 |
     10. | 1944    547.5      499.6 |
         |--------------------------|
     11. | 1945    561.2      547.5 |
     12. | 1946    688.1      561.2 |
     13. | 1947    568.9      688.1 |
     14. | 1948    529.2      568.9 |
     15. | 1949    555.1      529.2 |
         |--------------------------|
     16. | 1950    642.9      555.1 |
     17. | 1951    755.9      642.9 |
     18. | 1952    891.2      755.9 |
     19. | 1953   1304.4      891.2 |
     20. | 1954   1486.7     1304.4 |
         +--------------------------+

    Comment


    • #3
      Thanks!

      Comment

      Working...
      X