0
votes

I am using a for loop:

foreach n of numlist 1/7 {

Then doing various things like...

recode ${controls`n'} (.=0)
pscore intervention_family ${controls`n'}, pscore(ps_oowb) 
blockid(bl_oowb)

where

$controls`n' 

is a previously defined macro with a variables list in it.

E.g

global controls1 ///
age /// 
sex ///

This is all fine until I need to use another for loop as follows...

foreach var in $controls`n' {
  local i=`i'
    putexcel A`i'="`var'" B`i'="U" C`i'=`r(m1u_`var')' 
  D`i'=`r(m0u_`var')' 
  ......
}

I cannot put brackets round

$controls`n' 

here as then Stata says 'code after brace on same line' but without them Stata does not seem to recognise this global.

Previously I was simply calling a macro

foreach var in $controls {
    ...
}

And it worked fine. Anyone know the correct syntax / work around?

1
In your last block of code tne local macro i is not defined that we can see.Nick Cox

1 Answers

0
votes

Try to "unpack" this structure via:

foreach n of numlist 1/7 {
foreach var of global controls`n' {

}
}