Announcement

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

  • How to repeat some command lines

    Hello all,

    I need to repeat 3 line of commands in stata to create updated observations for three variables (FV1, ID10066, amort1) on my dataset, the calculation for the three happens depending on each other, therefore each row is needed for calculations of the next row. When I manually repeat these lines and run them then I get the desired result. However I look for a way to automatically do this. I also looked into writing a loop to do so but I could not figure out what should be done to get it write. The commands are the following: (first 4 observations are filled out already before getting to these lines of code)

    replace FV1 = FV1[_n-1] - amort1 if date > date[3]
    replace ID10066 = (FV1[_n-1])*rq if date > date[3]
    replace amort1 = pmt - ID10066 if date > date[4]

    Please let me know if I need to add extra information for clarity. But the purpose here is only to find a way to repeat these lines automatically instead of typing/running them again and again.

    Thank you all.

  • #2
    I do not think you can automate much this, because you do something different with every different variable.

    Comment


    • #3
      Originally posted by Joro Kolev View Post
      I do not think you can automate much this, because you do something different with every different variable.
      Thank you very much for your answer. Is then in general a loop which could be created for this? I tried the following for example:

      forvalues i=4/157 {
      replace FV1 = FV1[`i'-1] - amort1 if date > date[4]
      replace ID10066 = (FV1[`i'-1])*rq if date > date[4]
      replace amort1 = pmt - ID10066 if date > date[4]
      }

      but of course it doesn't work since I need the value of "amort1" for calculation of "FV1" and for amort1 I need "ID10066" which depends again on FV1. I would appreciate any possible improvements to the code that one might suggest.

      Comment


      • #4

        if the inside of the loop works and you need to loop over _n=4 to 157 then you need to replace only the singe observation when i== _n like

        forvalues i=4/157 {
        replace FV1 = FV1[`i'-1] - amort1 if date > date[4] & `i' ==_n
        replace ID10066 = (FV1[`i'-1])*rq if date > date[4] & `i' ==_n
        replace amort1 = pmt - ID10066 if date > date[4] & `i' ==_n
        }

        otherwise replace will replace all the observations in each loop

        Comment


        • #5
          Originally posted by Oscar Ozfidan View Post
          if the inside of the loop works and you need to loop over _n=4 to 157 then you need to replace only the singe observation when i== _n like

          forvalues i=4/157 {
          replace FV1 = FV1[`i'-1] - amort1 if date > date[4] & `i' ==_n
          replace ID10066 = (FV1[`i'-1])*rq if date > date[4] & `i' ==_n
          replace amort1 = pmt - ID10066 if date > date[4] & `i' ==_n
          }

          otherwise replace will replace all the observations in each loop
          Thank you very much. I found this helpful.

          Comment

          Working...
          X