Announcement

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

  • Using indented foreach and forvalues

    Dear all, I am trying for the first time to use foreach and forvalues, but with little success. The main issue is that I have two indented for (one on the firms, index i; one for the years, variable year), and I don't know precisely if and how I can do this.
    Basically, in a longitudinal sample, I want to identify firms that experienced a high decrease in sales (set decrease=2), low decreases (set decrease=1), no decrease (set decrease = 0), taking into account 5 years. To make things more complicated, I want to consider as "no decrease" also the firms that experienced just one decrease year-on-year, since one decrease may happen accidentally, so I need a variable to count how many times, in the five years, growth happened).

    Thanks for taking the time to help me

    Code:
    foreach i of firm
    {
        forvalues year=(2009(1)2013)
           {
             gen decrease`i'=2 if max(sales_growth`year,i')< -4
            //decrease 2 == HIGH DECREASE: max decrease of the i-th firm with respect to 2008  larger than 4%
            
           replace decrease`i'=1 if max(sales_growth`year,i')> -4 & max(sales_growth`t')< 0
            //decrease 1 == LOW DECREASE: max decrease  the i-th firm with respect to 2008  between 0 and 4%
    
          generate countgrowth`i'=0
          replace countgrowth=countgrowth`i'+1 if (sales_growth`year,i')> 0
          //I check how many times sales_growth of the i-th firm was positive
        }
    
    replace decrease`i'=0 if countgrowth`i'>3
    //if a firm's sales growth was always positive, countgrowth schould be 5, if only one decrease occcurred, it should be 4, we're good with both, since one accidental decrease may happen
    
    }

  • #2
    Code:
     
     foreach i of firm
    just won't work as the keyword of in foreach must always be followed by one of a selected number of keywords.

    Looking ahead local macro calls like

    Code:
     
     `year,i'
    are unfortunately wild guesses and don't correspond to any part of Stata syntax.


    The larger issue is that this isn't a looping problem at all -- or more precisely loops aren't needed as a solution. Indenting is irrelevant either way as indenting is nothing more than a presentation choice in showing code (although a very good choice).

    That said, I don't want to guess too much. I think you should show us a worked data example as requested in FAQ Advice #12. Invented data could be useful as real so long as they match your variable names and show how calculations are to be made.

    Comment


    • #3
      To add to Nick's comments, the documentation clearly says the { must appear on the forvalues or foreach line. If you'd tried to run the posted code, Stata would have responded "{ required" which is pretty clear.

      So, I assume you didn't bother to try to run your code before asking for help. We expect you to make appropriate efforts to solve your problem before asking and that includes reading the documentation.

      You also seem to want to have separate variables for each firm. This is seldom a good strategy with panel data. What Nick is hinting at is that you can probably do all of this with a few
      bysort firm: or bysort firm year: conditions. on a generate or egen.

      Comment

      Working...
      X