I am trying to run a program to generate bootstrapped indirect effects from an SEM model in STATA. The program runs fine by itself but when I insert it in a foreach loop, the end of the program is read by STATA as end of my do-file. Is there a way to avoid this? A possible solution is to write the program outside of the foreach loop but given I need to do this for each IV/DV combination, it would be a lot of separate programs to write. Looking for a more elegant solution.
Code:
foreach dv in dv1 dv2 dv3 { local i=1 foreach iv in iv1 iv2 iv3 { capture program drop IndirectModel`dv'`i' program IndirectModel`i', rclass syntax [if] [in] quiet sem (`iv' -> med1) (`iv' -> med2) (`iv' med1 med2 -> `dv') `if' `in' return scalar med1`iv' = [med1]_b[`iv']*[`dv']_b[med1] return scalar med2`iv' = [med2]_b[`iv']*[`dv']_b[med2] end //this end leads to r(1) error in stata - 'end of do-file' local i=`i'+1 } bootstrap r(med1`iv') r(med2`iv') , bca reps(5) nodots: IndirectModel`dv'`i' eststo model`iv' }
0 Response to writing a program within a foreach loop in STATA : avoiding error r(1)
Post a Comment