Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# st: Looping Enquiry

 From George Bouliotis <[email protected]> To "[email protected]" <[email protected]> Subject st: Looping Enquiry Date Thu, 29 Sep 2011 14:22:01 +0100

```Dear Stata users,

Although an old Stata user, currently I am doing my first steps in programming.

One of the parts in my programme tries (unsuccessfully) to replicate the column RESULT below. The difficulty is in how to loop a sequential product as, for instance: observ4= obs4 X obs3 (lag1) X obs2 (lag2)  X obs1 (lag1).

I tried some loops with "forvalue" but none was successful. I would appreciate any help with this.

George

Dr. George Bouliotis

Research Fellow in Biostatistics
MRC-MHTMR (Midland Hub for Trials Methodology Research)
University of Birmingham

#####################################
set more off
clear
input    time   group   var  RESULT
30       1        0  -0.958
57       0        0  -0.916
58       0        0  -0.834
67       0        0  -0.874
74       0        0  -0.792
79       0        0  -0.750
79       1        1   0.125
82       1        1   0.125
89       0        0  -0.706
95       1        0  -0.662
98       0        0  -0.678
101       0        0  -0.574
104       0        0  -0.532
110       0        0  -0.448
118       0        0  -0.444
end

list , clean

//Generating Ssize variable
egen ssize=seq() if var==0,from(47) to(1)
replace ssize=ssize[_n-1] if ssize==.
list, noobs clean

//Generating product variable
gen  product= (ssize/(ssize+1))

//Generating Score variable (PRODUCT)
gen  score= 1-(2*product) in 1/1 if var==0
// for the first observation only

//**REPLACEMENT A: when var==0
replace  score= 1-(2*(product*product[_n-1]))  if var==0 & score==.
// fine for the second obs only  (correct formula for when var=0)

//**REPLACEMENT B: when var==1
replace  i1= 1-1*(score*score[_n-1])  if var==1
// fine for the second observ only (correct formula for when var=1)
// but instead of [_n-1] I need a loop for [_n-`n(lagged)'] with "forvalue" command?

list, clean noobs

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