Dear colleagues, good afternoon

I have got a vertical dataset consisting of two variables ar and company. The number of companies is 37, each having 2,922 different abnormal returns. I created a few lines of code that aim to calculate the Corrado (1989) rank test value for each individual AR within the range of 244 to 2,907 (2,922 - 15). That is, I want to calculate the Corrado test statistic for each single point X (244;2,678) with an event window (-244;15).

As you see below, I use two loops (forvalues and foreach). When only running the code with the latter, it works in order calculate the focal point test statistic for one single point (e.g., for the abnormal return at time/point 244). However, when introducing the former loop to automate the calculations from point 244 to 2,907 it tells me that I have got an invalid syntax.

May I ask you to please be so kind as to have a look at it and point me in the right direction? Thanks!

Florian

-----------------------------------------------------------------------------------------------------------------------------------
gen corrado_test=.
sort company time
by company: gen nvals = _n==1
egen company_all = sum(nvals)
drop nvals
sort company time
by company: gen no=_n

forvalues q in 1/2907 {
by company: egen rank=rank(ar) if inrange(no, `q', `q'+260)
gen step3=rank-130.5
gen step4a=.

foreach i of num 1/260 {
by company: gen noi=_n
replace step4a=sum(step3) if noi==`i'
drop noi
}

replace step4a=. if company!=company[_N]
gen step4b=step4a/company_all
gen step4c=step4a/12
gen step5=step4c^2
egen step6=sum(step5)
replace step6=. if step5==.
gen step7=step6/260
gen step8=sqrt(step7)
gen step9=step4b/step8
replace corrado_test=step9 if no==`q'+244
}
-----------------------------------------------------------------------------------------------------------------------------------