Announcement

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

  • Refer to a value for a specific date

    Hi everyone,
    I need to refer to a value at a specific date in a loop. For instance to calculate the constant instalment of a loan (Instalment variable) I need to refer in all periods (30 months) to the principal as of January 2022, set in the variable date. I am using a loop for 2 scenarios. How can I set in the loop the principal amount as of January 2022 for all periods?

    Thank you all for the help

  • #2
    https://www.stata-journal.com/articl...article=dm0055 covers related tricks. Something like

    Code:
    egen principal_jan_2022 = mean(cond(date == ym(2022, 1), principal, .))
    or

    Code:
    egen principal_jan_2022 = mean(cond(date == ym(2022, 1), principal, .)) , by(id)
    should get you closer.

    Here's a reproducible example:


    Code:
    . webuse grunfeld , clear
    
    . su year
    
        Variable |        Obs        Mean    Std. dev.       Min        Max
    -------------+---------------------------------------------------------
            year |        200      1944.5    5.780751       1935       1954
    
    . egen invest1939 = mean(cond(year == 1939, invest, .)) , by(company)
    
    . l company year invest* in 1/30
    
         +------------------------------------+
         | company   year   invest   inv~1939 |
         |------------------------------------|
      1. |       1   1935    317.6      330.8 |
      2. |       1   1936    391.8      330.8 |
      3. |       1   1937    410.6      330.8 |
      4. |       1   1938    257.7      330.8 |
      5. |       1   1939    330.8      330.8 |
         |------------------------------------|
      6. |       1   1940    461.2      330.8 |
      7. |       1   1941      512      330.8 |
      8. |       1   1942      448      330.8 |
      9. |       1   1943    499.6      330.8 |
     10. |       1   1944    547.5      330.8 |
         |------------------------------------|
     11. |       1   1945    561.2      330.8 |
     12. |       1   1946    688.1      330.8 |
     13. |       1   1947    568.9      330.8 |
     14. |       1   1948    529.2      330.8 |
     15. |       1   1949    555.1      330.8 |
         |------------------------------------|
     16. |       1   1950    642.9      330.8 |
     17. |       1   1951    755.9      330.8 |
     18. |       1   1952    891.2      330.8 |
     19. |       1   1953   1304.4      330.8 |
     20. |       1   1954   1486.7      330.8 |
         |------------------------------------|
     21. |       2   1935    209.9      230.4 |
     22. |       2   1936    355.3      230.4 |
     23. |       2   1937    469.9      230.4 |
     24. |       2   1938    262.3      230.4 |
     25. |       2   1939    230.4      230.4 |
         |------------------------------------|
     26. |       2   1940    361.6      230.4 |
     27. |       2   1941    472.8      230.4 |
     28. |       2   1942    445.6      230.4 |
     29. |       2   1943    361.6      230.4 |
     30. |       2   1944    288.2      230.4 |
         +------------------------------------+
    
    .

    Comment

    Working...
    X