I am using putexcel in tata 15.1. have an issue exporting results to Excel through multiple loops.
The following code does what I need before adding a loop for levelsof of the strata variables

[/CODE]
cap putexcel close
putexcel set xlsexport, replace
putexcel A1=("`v'"), bold
putexcel B1="IRR"
putexcel C1="95%CI LB"
putexcel D1="95%CI UB"

local outcomes dvar1 dvar2
local controls control1
local row = 2

foreach v in `outcomes' {
poisson `v' i.ind `controls', irr vce(robust)
qui putexcel close
qui putexcel set xlsexport, modify open
putexcel A`row'=("`v'"), bold
putexcel B`row'=(el(r(table), 1, 2)) //COEF VALUES (IRR)
putexcel C`row'=(el(r(table), 5, 2)) //95%CI LB
putexcel D`row'=(el(r(table), 6, 2)) //95%CI UB
local ++row
}
putexcel close

[/CODE]

I need to repeat the same model for each levels of the strata variables (see following code), but I get lost on how to export the results in Excel (I have removed the previous pieces of putexcel code, now not working, to not add to the confusion).

Code:
cap putexcel close
putexcel set xlsexport_levelsof, replace
putexcel A1=("`v'"), bold
putexcel B1="IRR"
putexcel C1="95%CI LB"
putexcel D1="95%CI UB"

local outcomes dvar1 dvar2 
local controls control1 
local strata levelvar1 levelvar2
local row = 2

foreach v in `outcomes' {
  foreach st in `strata'{
  levelsof `st', local(levels)
  foreach l of local levels {
    di "`v' if `st'==`l'"
    poisson `v' i.ind `controls' if `st'==`l', irr vce(robust)
   qui putexcel close
   qui putexcel set xlsexport, modify open
   *removed putexcel code
    }
  }
}
putexcel close
Any hint on how to deal with this problem would be greatly appreciate!
Many thanks,
Martin


Data
Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input byte(dvar1 dvar2 ind control1 levelvar1 levelvar2)
1 1 1 31 1 2
2 1 1 33 1 6
1 1 0 24 0 6
1 0 0 18 0 13
1 0 1 20 1 12
1 1 1 22 3 5
2 0 1 56 1 5
1 0 0 38 0 6
1 0 1 41 1 6
1 1 0 34 0 7
2 0 1 22 1 16
1 0 1 22 1 12
1 0 1 21 1 6
1 1 0 66 0 1
1 1 1 42 1 6
1 1 1 30 1 6
1 1 0 18 0 7
1 1 0 32 3 6
1 1 0 27 0 6
1 0 1 25 1 3
1 1 0 28 1 16
1 1 0 41 3 6
1 1 1 19 3 0
1 1 1 68 1 17
1 1 1 54 1 6
1 1 1 25 1 3
1 1 1 24 1 6
1 0 0 20 0 6
1 1 0 25 0 4
1 1 0 30 0 16
2 0 0 22 0 15
1 0 0 44 0 6
1 1 1 33 1 7
1 1 1 48 1 6
1 1 0 21 1 3
1 1 0 44 0 4
1 0 0 22 1 6
2 1 0 47 1 15
1 0 0 23 1 5
1 0 0 25 0 7
1 1 0 20 0 16
1 1 0 24 0 6
1 1 0 60 0 6
1 1 1 71 1 6
1 1 0 25 3 6
1 1 0 31 0 1
1 1 1 32 1 7
2 0 1 42 1 6
1 1 0 47 0 16
1 1 0 19 0 15
1 1 1 42 1 6
1 0 0 20 0 7
1 1 0 65 0 14
1 1 0 41 0 16
1 1 0 23 0 14
1 1 1 21 1 6
2 1 1 24 1 6
1 1 1 27 1 6
1 0 1 35 3 6
1 1 1 42 1 7
1 1 0 41 0 6
1 0 0 62 0 6
1 0 1 27 1 6
1 1 0 26 0 6
1 1 0 46 3 6
1 0 0 22 0 16
1 1 0 28 3 7
1 1 0 54 0 6
1 1 1 53 1 6
1 1 1 57 1 6
1 1 1 65 1 13
1 1 1 64 1 6
1 1 1 51 1 3
1 0 1 57 1 6
1 0 1 56 1 12
1 1 1 57 1 6
1 1 1 67 1 6
1 1 0 46 0 6
1 0 0 68 0 16
1 1 1 37 1 3
1 1 1 57 3 6
1 1 0 37 0 6
1 1 1 68 1 2
1 1 0 18 1 15
1 1 0 25 0 17
1 0 1 53 1 6
1 0 0 26 0 13
1 1 0 22 0 6
1 0 1 23 1 7
1 1 1 34 1 6
1 1 1 26 1 6
1 1 0 19 0 12
2 1 0 22 0 15
1 1 1 46 1 3
1 1 1 25 1 4
1 0 0 21 0 6
2 0 0 26 1 16
1 1 0 27 0 6
1 1 0 32 3 6
1 1 1 52 0 12
end