For context, I am attempting to conduct a moderated mediation analysis in Stata ala https://stats.oarc.ucla.edu/stata/faq/how-can-i-do-moderated-mediation-in-stata/. As part of the procedures to estimate indirect effects, I eventually will be multiplying the coefficient of a predictor on the outcome with the coefficient of a predictor on a mediator. Because I will have multiple predictor variables, mediators, and moderators I would like to copy regression coefficients from separate regressions into global macros generated in a do foreach loop so that I can refer to them with mnemonic names, However, I am referring to the content of the global macros incorrectly in the loop.
Here is a fragment of a program that is supposed to generate the global macros and display the macro name and it's content.
macro list shows that the global macros were saved correctly
However, When I run the program, I am getting a macro not found error because I am referring to it incorrectly in the loop.
I must be using quotes incorrectly. in the portion of the loop where I attempt to refer to the content of the global macro "$pos_`var'_d". The "pos_ portion of the macro name is dropped and I get the interval_d not found error. I've read the documentation on using single and double quotes and Nick Cox's tutorials on this, but clearly am not understanding. I'd appreciate any help.
Here is a fragment of a program that is supposed to generate the global macros and display the macro name and it's content.
Code:
. global objective interval length . capture program drop boottest . program boottest, rclass 1. quietly xtreg emopos $objective, fe 2. . foreach var of varlist $objective { 3. return scalar pos_`var'_d = _b[`var'] 4. global pos_`var'_d = _b[`var'] 5. di "pos_`var'_d", $pos_`var'_d 6. } 7. end
Code:
. macro list pos_length_d: .0191397788464435 pos_interval_d: .0204940257696814
Code:
. boottest
pos_interval_d interval_d not found
Comment