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

}