Dear Statalists,
I would like to consult with you about using Macros and Foreach Loops to perform Bootstrap for mediation analysis, in STATA 17. My goal is to estimate the indirect effects of X2 on Y through the channel X1. My code is:
Now, my problem arises when I try to generalize the codes because my goal now is to estimate a set of indirect effects of X2 on each of Y1, Y2, Y3. Therefore, I consider using Macros and Foreach loop. Here is what I code:
However, STATA returns as "invalid syntax"
I wish to listen to your expertise. Meanwhile, How does the code look like if X2 becomes a set of variables X21, X22, X23,...?
Thanks so much!
I would like to consult with you about using Macros and Foreach Loops to perform Bootstrap for mediation analysis, in STATA 17. My goal is to estimate the indirect effects of X2 on Y through the channel X1. My code is:
Code:
encode CompanyName, gen(id)
xtset id Year
capture program drop channel
program define channel, rclass
xtreg X1 X2 CONTROLS i.Year, vce(cluster id2)
scalar a = _b[X2]
xtreg Y X1 X2 CONTROLS i.Year, vce(cluster id2)
scalar b = _b[X1]
return scalar indirect = a*b
end
bootstrap r(indirect), reps(1000) idcluster(id) cluster(CompanyName) seed(12345): channel
Code:
capture program drop channel
program define channel, rclass
xtreg X1 X2 CONTROLS i.Year, vce(cluster id2)
scalar a = _b[X2]
xtreg `Y' X1 X2 CONTROLS i.Year, vce(cluster id2)
scalar b = _b[X1]
return scalar indirect = a*b
end
foreach Y of varlist Y1 Y2 Y3{
bootstrap r(indirect), reps(1000) idcluster(id) cluster(CompanyName) seed(12345): channel}
I wish to listen to your expertise. Meanwhile, How does the code look like if X2 becomes a set of variables X21, X22, X23,...?
Thanks so much!

Comment