Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.

# st: RE: foreach/forvalue-loop

 From "Modesto Escobar" To Subject st: RE: foreach/forvalue-loop Date Sun, 14 Apr 2013 10:03:30 +0200

I would convert your code into this one:

local dif1 -10 -5 0 2
local dif2 -6 -1 1 5
local wind 3 4 5 6
forvalues i=1/4 {
local p1: word `i' of `dif1'
local p2: word `i' of `dif2'
local pp: word `i' of `wind'
egen estimationt_test_window`pp' = sum(averagedeltachange) if dif >=`p1' & dif <=`p2' & id==1
}

But, maybe you could want one more simple task like this:
recode dif (-10/-6=1)(-5/-1=2)(0/1=3)(2/5=4) (else=.), into(ddif)
tabstat averagedeltachange , statistics(sum) by(ddif)

I hope to be useful.

Modesto Escobar
modesto@usal.es

-----Mensaje original-----
De: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] En nombre de Künzel Sven
Enviado el: sábado, 13 de abril de 2013 23:05
Para: statalist@hsphsun2.harvard.edu
Asunto: st: foreach/forvalue-loop

Dear Statalist,

I tried to program a code. An excerpt from the code can be seen below.
As you can see I tried to program a part of a t-test. At first I program several "windows" which have different durations. As a second step I summarize the "averagedeltachange" for every window. And as a third I drop the variable "t_test_window". As you can see I am reproducing the same code for every window. Now I would like to abbreviate the length of the code by using a foreach/forvalue-loop but I am not quite sure if this is possible at all? And how it would have to look like?

Thanks for helping me.

/*window t=-10 bis t=-6*/
gen t_test_window=1 if dif >=-10 & dif <=-6 & id==1 egen estimationt_test_window3 = sum(averagedeltachange) if
t_test_window==1
drop t_test_window

/*window t=-5 bis t=-1*/
gen t_test_window=1 if dif >=-5 & dif <=-1 & id==1 egen estimationt_test_window4 = sum(averagedeltachange) if
t_test_window==1
drop t_test_window

/*window t=0 bis t=1*/
gen t_test_window=1 if dif >=0 & dif <=1 & id==1 egen estimationt_test_window5 = sum(averagedeltachange) if
t_test_window==1
drop t_test_window

/*window t=2 bis t=5*/
gen t_test_window=1 if dif >=2 & dif <=5 & id==1 egen estimationt_test_window6 = sum(averagedeltachange) if
t_test_window==1
drop t_test_window

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/