Dear Statalisters,
I want to create a local containing sets of explanatory variables which are themselves stored as locals. The local is intended as an input to a program. Doing this manually works. However, I would like to automize this.
My strategy is to create the local via a loop and adding each set of explanatory variables in turn (controls_1 - controls_3). What I want to end up with is a "string of strings". However, I have trouble with how to deal with the compound double quotes necessary for that.
Below I post a MWE. My main problem is the foreach loop populating `whatIget'. Does any of you have a hint on the correct use of compound double quotes to make `whatIget' equivalent to `whatIwant'?
Any help is much appreciated!
Ben
I want to create a local containing sets of explanatory variables which are themselves stored as locals. The local is intended as an input to a program. Doing this manually works. However, I would like to automize this.
My strategy is to create the local via a loop and adding each set of explanatory variables in turn (controls_1 - controls_3). What I want to end up with is a "string of strings". However, I have trouble with how to deal with the compound double quotes necessary for that.
Below I post a MWE. My main problem is the foreach loop populating `whatIget'. Does any of you have a hint on the correct use of compound double quotes to make `whatIget' equivalent to `whatIwant'?
Any help is much appreciated!
Ben
Code:
** minimum working example ssc install estout // gen fake data clear set obs 100 gen depvar = runiform(0,1) gen x1 = runiform(0,1) gen x2 = runiform(0,1) gen x3 = runiform(0,1) // sets of explanatory variables local controls_1 `"x1"' local controls_2 `"x2"' local controls_3 `"x1 x2 x3"' // local selecting the sets of explanatory variables I want to pass to the function local controlset 1 2 3 // manually, this works local whatIwant `"`"x1"' `"x2"' `"x1 x2 x3"'"' di `whatIwant' // trying to automize whatIwant: local whatIget foreach set in `controlset' { di "`controls_`set''" local whatIget `whatIget' `controls_`set'' } di "`whatIget'" ** testing the two versions (final use intended is to pass this to a program, example below just for clarification of the problem) // testing whatIwant foreach model in `whatIwant' { quietly: eststo: reg depvar `model' } esttab // want: 3 models, get: 3 models eststo clear // testing whatIget foreach model in `whatIget' { quietly: eststo: reg depvar `model' } esttab // want: 3 models, get: 5 models eststo clear
Comment