Announcement

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

  • Creating loops with conditions

    Goodmorning everyone,
    I'm here again to ask you for help.

    My starting code is this:
    Code:
    gen pROE2019=5*(ROE2019Norm<.)
    gen pROE2018=4*(ROE2018Norm<.)
    gen pROE2017=3*(ROE2017Norm<.)
    gen pROE2016=2*(ROE2016Norm<.)
    gen pROE2015=1*(ROE2015Norm<.)
    
    gen pROI2019=5*(ROI2019Norm<.)
    gen pROI2018=4*(ROI2018Norm<.)
    gen pROI2017=3*(ROI2017Norm<.)
    gen pROI2016=2*(ROI2016Norm<.)
    gen pROI2015=1*(ROI2015Norm<.)
    
    gen pLEVERAGE2019=5*(LEVERAGE2019Norm<.)
    gen pLEVERAGE2018=4*(LEVERAGE2018Norm<.)
    gen pLEVERAGE2017=3*(LEVERAGE2017Norm<.)
    gen pLEVERAGE2016=2*(LEVERAGE2016Norm<.)
    gen pLEVERAGE2015=1*(LEVERAGE2015Norm<.)
    I would like to turn it into a loop and I thought about this type of code:
    Code:
    local YearN=2019
    forvalues y=`=`YearN'-4'(1)`YearN' {
        forvalues i=1(1)5 {
    gen pROE`y'=`i'*(ROE`y'Norm<.)
    gen pROI`y'=`i'*(ROI`y'Norm<.)
    gen pLEVERAGE`y'=`i'*(LEVERAGE`y'Norm<.)
        }
    }
    do you think this procedure is correct?
    Thanks again for the help and support you provide!

  • #2
    No, it's not correct. You can see that quickly because with the nested loops you have, you are asking Stata to combine each year from 2015 through 2019 with each value of 8 from 1 through 5. That makes 25 generate statements each for ROE, ROI and LEVERAGE, whereas you only want 5 each. Now, actually, you won't get all those 25 because when you get to `i' == 2, your -gen...- statements will throw an error because the variables pROE* pROI* and pLEVERAGE* will already exist, and the code will halt.

    So you need for the looping on i and y to be parallel, not nested. In fact, in this case, it is simplest to just use a single index, and calculate the value of the other from it. While we are at it, what you are doing with ROE is exactly analogous to what you are doing with ROI, and LEVERAGE also is treated the same way. So you can simplify it by doing a nested loop structure, but one where the outer loop is over the stubs ROE, ROI, and LEVERAGE.

    Code:
    foreach x in ROI ROE LEVERAGE {
        forvalues i = 1/5 {
            gen p`x'`=2014+`i'' = `i'*(`x'`=2014+`i'' < .)
        }
    }

    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      No, it's not correct. You can see that quickly because with the nested loops you have, you are asking Stata to combine each year from 2015 through 2019 with each value of 8 from 1 through 5. That makes 25 generate statements each for ROE, ROI and LEVERAGE, whereas you only want 5 each. Now, actually, you won't get all those 25 because when you get to `i' == 2, your -gen...- statements will throw an error because the variables pROE* pROI* and pLEVERAGE* will already exist, and the code will halt.

      So you need for the looping on i and y to be parallel, not nested. In fact, in this case, it is simplest to just use a single index, and calculate the value of the other from it. While we are at it, what you are doing with ROE is exactly analogous to what you are doing with ROI, and LEVERAGE also is treated the same way. So you can simplify it by doing a nested loop structure, but one where the outer loop is over the stubs ROE, ROI, and LEVERAGE.

      Code:
      foreach x in ROI ROE LEVERAGE {
      forvalues i = 1/5 {
      gen p`x'`=2014+`i'' = `i'*(`x'`=2014+`i'' < .)
      }
      }
      Thanks for your help. Now the code runs correctly!

      Comment

      Working...
      X