Announcement

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

  • Loop in a Loop

    Hi everybody, I started learning stata recently and I cannot overcome this error.
    I am trying to create N portfolios (373) where in each portfolio I have insert the sum of different variables (varlist from month_au to month_us) following two conditions:
    if the sum of the past 12months is positive (this sum is stored in the variable returnsmonth_*), then I will have to add the specific variable
    if the sum of the past 12months is negative, then I will have to substract the specific variable

    I tried to use the following code;

    forvalues i = 1(1)373 {
    gen portfolio`i' = 0
    foreach var of varlist month_au - month_us {
    replace portfolio`i' = portfolio`i' + `var' if returns`var'[Nobs -1] > 0
    replace portfolio`i' = portfolio`i' - '`var' if returns`var'[Nobs -1] < 0
    }
    }

    however, it reports an error saying "month_au invalid name"

    I also tried to use a local variables giving to y the values from 1 to 373 but it does not store any results.

    foreach i of local y {
    gen portfolio`i' = 0
    foreach var of varlist month_au - month_us {
    replace portfolio`i' = portfolio`i' + `var' if returns`var'[Nobs -1] > 0
    replace portfolio`i' = portfolio`i' - '`var' if returns`var'[Nobs -1] < 0
    }
    }

    Hoping the problem is clear, any help will be more that appreciate,
    Thanks in advace!

  • #2
    There is at least one typographical error in your code.
    Code:
    replace portfolio`i' = portfolio`i' - '`var' if returns`var'[Nobs -1] < 0
    should be
    Code:
    replace portfolio`i' = portfolio`i' - `var' if returns`var'[Nobs -1] < 0

    Comment


    • #3
      you're right, but apart from that the main error is related to the second foreach, since it does not recognize the variables

      Comment


      • #4
        As your code is written each portfolio variable will be identical as the code is the same for each of 1 to 373.

        If data for different months are in different variables that implies that you need to reshape long.

        But these are guesses as I don't understand your problem otherwise.



        Comment


        • #5
          the main error is related to the second foreach, since it does not recognize the variables
          Apparently you did not correct the error and rerun the code, but instead chose to assert that the typographical error I reported could not be the source of your problem.
          Code:
          . sysuse auto
          (1978 automobile data)
          
          . foreach var of varlist price - rep78 {
            2. display 1+'`var'
            3. }
          'price invalid name
          r(198);
          however, it reports an error saying "month_au invalid name"
          No, apparently it reports an error saying
          Code:
          'month_au invalid name
          Note carefully that the error message includes the typographical error as part of the invalid name, so in fact it told you exactly what was wrong.
          Last edited by William Lisowski; 22 Jan 2022, 11:38.

          Comment


          • #6
            you were right, now it seems to work, thank you very much!

            Comment

            Working...
            X