# RE: st: Problem creating multiple loops

 From "Nick Cox" To Subject RE: st: Problem creating multiple loops Date Sun, 15 Jun 2008 17:59:40 +0100

```A wild guess is that Hugh would be much better off reshaping his data from wide to long.

Nick
n.j.cox@durham.ac.uk

Carlos Eduardo Hernández Castillo

I reckon this should work:

forvalues byear = 1993(1)2002 {
forvalues cont = 1(1) 4 {
local year=`byear'+`cont'
gen var1_`cont' = ln(((var1_`year'-var1_`byear')/var1_`byear')+1) if
year==`byear'
}
}

Hugh Colaco

I need a more efficient code than the
> one below. I tried various combinations of -local-, -foreach- and
> -forval-, but am unable to get it right.
>
>
>       gen var1_1 = ln(((var1_1994-var1_1993)/var1_1993)+1) if year==1993;
>
>       gen var1_2 = ln(((var1_1995-var1_1993)/var1_1993)+1) if year==1993;
>
>       gen var1_3 = ln(((var1_1996-var1_1993)/var1_1993)+1) if year==1993;
>
>       gen var1_4 = ln(((var1_1997-var1_1993)/var1_1993)+1) if year==1993;
>
>
>       replace var1_1 = ln(((var1_1995-var1_1994)/var1_1994)+1) if year==1994;
>
>       replace var1_2 = ln(((var1_1996-var1_1994)/var1_1994)+1) if year==1994;
>
>       replace var1_3 = ln(((var1_1997-var1_1994)/var1_1994)+1) if year==1994;
>
>       replace var1_4 = ln(((var1_1998-var1_1994)/var1_1994)+1) if year==1994;
>
>
>       ............
>
>       ............
>
>       ............
>
>       replace var1_1 = ln(((var1_2003-var1_2002)/var1_2002)+1) if year==2002;
>
>       replace var1_2 = ln(((var1_2004-var1_2002)/var1_2002)+1) if year==2002;
>
>       replace var1_3 = ln(((var1_2005-var1_2002)/var1_2002)+1) if year==2002;
>
>       replace var1_4 = ln(((var1_2006-var1_2002)/var1_2002)+1) if year==2002;

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```