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

# st: local macro no longer available after some cycle in a forvalues

 From lorenzo baldini <[email protected]> To <[email protected]> Subject st: local macro no longer available after some cycle in a forvalues Date Thu, 26 Jul 2012 22:57:22 +0000

```Hello,
i created loops for make a matching between a variable and index. the index change with the dipendent variable and, moreover, the computation of index is done by relaxing a constraint if i find that the first formulation (not relaxed) isn't able to create the index itself, and in case furthe relaxing and so on.
i find that when stata finds an index for each observation (after some cycle in the third forvalues), then the macro "disappears" so return to me an "invalid syntax" error. so i try to don't compute the index when the index is complete by an if condition on that local (if `rmax'!=.) but the same problem remains. So i've no idea what is the problem.
the following is the code:

sort day id_firm
gen week=week(day)
gen year=year(day)
egen x=group(week year)
drop year week
compress
sort day id_firm
gen step=.               /* Step will track at which step (1,2,3 and potentially 4) the index is computed */
qui sum x, meanonly
local samp=`r(max)'
save \$mc_file, replace

// as it is coded now, it should take about an hour to compute the index for each firm-day obs

/// NEW CODE

timer on 1
qui {
forvalues j=1/`samp' {
use \$mc_file, clear
keep if x==`j'
drop x
gen n=_n
save tmps_`j', replace

/// FIRST STEP
sum n, meanonly
forvalues i=1/`r(max)' {
local day_target=day[`i']
local slope_target=slope_pre[`i']
local slope_min=\$low_bound*`slope_target'
local slope_max=\$high_bound*`slope_target'

sum cds_pre if day==`day_target' & cds_pre>=`spread_min' & cds_pre<=`spread_max' & slope_pre>=`slope_min' & slope_pre<=`slope_max'  & n!=`i', meanonly
replace index_pre=r(mean) if n==`i' & index_pre==.
replace nf_index=r(N) if n==`i' & nf_index==. & index_pre!=.

sum cds_post if day==`day_target' & cds_pre>=`spread_min' & cds_pre<=`spread_max' & slope_pre>=`slope_min' & slope_pre<=`slope_max'  & n!=`i', meanonly
replace index_post=r(mean) if n==`i' & index_post==.
}
replace step=1 if index_pre!=. & index_post!=. & step==.

/// SECOND STEP : Less restrictive boundary on Slope

gen missing = (index_pre==. | index_post==.)
bysort missing: gen nn=_n                        /* sequential identifier of obs. with the index still missing */
replace nn=. if missing==0
replace index_pre=. if missing==1            /* be sure that if you have the index you have it for both days of interest (probably useless check) */
replace index_post=. if missing==1
drop missing
sort nn
sum nn, meanonly
if `r(max)'!=. {
forvalues k=1/`r(max)' {
sum n if nn==`k', meanonly                /* it finds for which observation we have to compute the index */
local i=r(mean)
local day_target=day[`i']
local slope_target=slope_pre[`i']
local slope_min=\$low_bound2*`slope_target'
local slope_max=\$high_bound2*`slope_target'

sum cds_pre if day==`day_target' & cds_pre>=`spread_min' & cds_pre<=`spread_max' & slope_pre>=`slope_min' & slope_pre<=`slope_max'  & n!=`i', meanonly
replace index_pre=r(mean) if n==`i' & index_pre==.
replace nf_index=r(N) if n==`i' & nf_index==. & index_pre!=.

sum cds_post if day==`day_target' & cds_pre>=`spread_min' & cds_pre<=`spread_max' & slope_pre>=`slope_min' & slope_pre<=`slope_max'  & n!=`i', meanonly
replace index_post=r(mean) if n==`i' & index_post==.
}
}

replace step=2 if index_pre!=. & index_post!=. & step==.

/// THIRD STEP: Less restricitve boundary on both Spread and Slope

gen missing = (index_pre==. | index_post==.)
bysort missing: gen nnn=_n                        /* sequential identifier of obs. with the index still missing */
replace nnn=. if missing==0
replace index_pre=. if missing==1            /* be sure that if you have the index you have it for both days of interest (probably useless check) */
replace index_post=. if missing==1
drop missing
sort nnn
sum nnn, meanonly

if `r(max)'!=. {
forvalues z=1/`r(max)' {
sum n if nnn==`z', meanonly
local i=r(mean)
local day_target=day[`i']
local slope_target=slope_pre[`i']
local slope_min=\$low_bound2*`slope_target'
local slope_max=\$high_bound2*`slope_target'

sum cds_pre if day==`day_target' & cds_pre>=`spread_min' & cds_pre<=`spread_max' & slope_pre>=`slope_min' & slope_pre<=`slope_max'  & n!=`i', meanonly
replace index_pre=r(mean) if n==`i' & index_pre==.
replace nf_index=r(N) if n==`i' & nf_index==. & index_pre!=.

sum cds_post if day==`day_target' & cds_pre>=`spread_min' & cds_pre<=`spread_max' & slope_pre>=`slope_min' & slope_pre<=`slope_max'  & n!=`i', meanonly
replace index_post=r(mean) if n==`i' & index_post==.
}
}

replace step=3 if index_pre!=. & index_post!=. & step==.

drop n nn nnn
save tmps_`j', replace
}
/* stack all subsamples results */
use tmps_1, clear
forvalues j=2/`samp' {
append using tmps_`j', nolabel
}
sort id_firm day
label var step "index computed at this step"
save \$mc_file, replace

/* eliminate subsamples */
forvalues j=1/`samp' {
erase tmps_`j'.dta
}
}
timer off 1

and this is the error:
- replace index_pre=r(mean) if n==`i' & index_pre==.
= replace index_pre=r(mean) if n==197 & index_pre==.
- replace nf_index=r(N) if n==`i' & nf_index==. & index_pre!=.
= replace nf_index=r(N) if n==197 & nf_index==. & index_pre!=.
- sum cds_post if day==`day_target' & cds_pre>=`spread_min' & cds_pre<=`spread_max' & slope_pre>=`slope_min' & slope_pre<=`slope_max' & n!=`i', meanonly
= sum cds_post if day==16442 & cds_pre>=16.4699993133545 & cds_pre<=20.12999916076661 & slope_pre>=58.55999755859375 & slope_pre<=87.83999633789063 & n!=
> 197, meanonly
- replace index_post=r(mean) if n==`i' & index_post==.
= replace index_post=r(mean) if n==197 & index_post==.
- }
- }
- replace step=2 if index_pre!=. & index_post!=. & step==.
- gen missing = (index_pre==. | index_post==.)
- bysort missing: gen nnn=_n
- replace nnn=. if missing==0
- replace index_pre=. if missing==1
- replace index_post=. if missing==1
- drop missing
- sort nnn
- sum nnn, meanonly
- if `r(max)'!=. {
= if !=. {
=:  operator invalid
forvalues z=1/`r(max)' {
sum n if nnn==`z', meanonly
local i=r(mean)
local day_target=day[`i']
local slope_target=slope_pre[`i']