Hello,

I'm trying to export a number of tables into excel. I'm trying to build a nested forloop in which I take only the results of a simple tab command, put it in excel in column B2 and then go for the next tabulation and put the result in C2 and so on. I want to build a table 3x12 in excel where rows correspond to a country and columns to 11 characteristics. my code, which doesn't work now looks like this:



Code:
putexcel set "C:\Users\STATA_results_stats\Zam_Nep_Viet\tab.xlsx"
putexcel A2=("Nepal") A3=("Vietnam") A4=("Zambia") 
putexcel B1 = ("Food") C1= ("Fridge") D1=("Solid shelter") E1=("Min floor space") F1=("Safe water") G1=("Toilet") H1=("Clean cooking dev.") I1=("Phone") J1=("TV") K1=("Electricity") L1=("Education") 

local vlist `c(alpha)'
foreach var of local vlist {
forval n = 1/11 {
  tabulate country_id  char_`n'  if char_`n'==1 , matcell(f_`n') matrow(names)
  putexcel  `var'2= matrix(f_`n') 
  }
  }
I know it goes 11 times through each variable but I don't know how to write the code so it goes only once. And I do not know how to start alphabet from B and finish on L. Is it possible in Stata? Thank you for your help!

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input float(country_id char_1 char_2 char_3 char_4 char_5 char_6 char_7 char_8 char_9 char_10 char_11)
1 1 0 1 1 1 1 1 0 1 1 0
1 0 0 0 0 1 0 0 0 0 0 0
1 1 1 1 0 1 1 1 1 1 1 0
1 1 0 0 0 1 0 0 1 0 1 0
1 0 0 0 1 0 1 0 0 0 1 0
1 1 0 0 1 0 0 0 0 0 1 0
1 1 0 1 1 1 1 0 1 1 1 0
1 1 0 1 0 1 0 0 1 1 1 0
1 1 0 1 0 1 1 1 1 1 1 0
1 1 0 0 1 1 1 0 1 1 1 0
1 1 0 0 1 1 0 0 0 1 1 0
1 1 0 1 1 1 1 1 1 1 1 0
1 1 0 1 0 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 0 1 1 0 1 1 1 0
1 1 0 0 1 1 1 1 1 1 1 0
1 1 0 0 1 1 0 0 0 0 0 0
1 1 0 1 0 1 0 0 0 0 1 0
1 1 0 0 0 1 0 0 0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 0 0 0 0 0 0 0
3 0 0 0 1 0 0 0 0 0 0 0
3 0 0 0 1 1 1 0 1 1 1 1
3 0 0 0 1 1 0 0 0 0 0 0
3 0 0 0 0 1 0 0 0 0 0 0
3 1 0 0 1 1 0 0 0 0 0 1
3 0 0 0 0 0 0 0 1 1 0 0
3 0 0 1 1 1 1 1 1 1 1 1
3 1 0 0 0 1 1 0 1 0 0 0
3 0 0 0 1 0 0 0 0 0 0 0
3 1 0 0 1 1 0 0 1 0 0 1
3 1 1 1 1 1 1 1 1 1 1 1
3 1 0 0 1 1 0 0 1 0 0 0
3 0 0 0 0 1 0 0 0 0 0 0
3 1 0 1 1 1 1 0 1 1 0 1
3 1 1 0 0 1 1 0 1 1 1 1
3 1 1 0 1 1 1 0 1 1 1 0
3 1 0 0 0 1 0 0 1 0 0 0
3 1 0 0 1 1 0 0 1 0 0 1
3 0 0 0 0 1 0 0 1 0 0 0
3 1 0 0 1 1 0 0 1 0 0 0
3 0 0 0 0 1 0 0 1 0 0 0
3 1 1 1 1 1 1 1 1 1 1 1
3 0 0 0 1 0 0 0 0 0 0 0
3 1 0 0 1 1 0 0 1 0 0 0
3 1 0 1 1 1 1 0 1 1 1 1
3 0 0 0 1 0 0 0 0 0 0 0
3 0 0 1 1 1 1 0 1 1 1 0
3 1 0 0 1 1 1 1 1 1 1 0
3 1 0 0 1 0 0 0 1 0 0 0
3 0 0 0 0 1 0 0 0 0 0 0
3 0 0 1 1 1 1 0 0 0 1 0
3 1 0 0 0 0 0 0 1 0 0 0
3 0 0 0 1 1 0 0 1 0 0 0
3 0 0 0 0 1 0 0 0 0 0 0
3 1 1 1 1 1 1 1 1 1 1 1
3 0 0 0 1 0 0 0 1 0 0 0
3 1 0 0 1 1 0 0 1 0 0 1
3 1 1 1 1 1 1 1 1 1 1 1
end
label values country_id country
label def country 1 "Nepal", modify
label def country 2 "Vietnam", modify
label def country 3 "Zambia", modify
label values char_1 yes_no
label values char_2 yes_no
label values char_3 yes_no
label values char_4 yes_no
label values char_5 yes_no
label values char_6 yes_no
label values char_7 yes_no
label values char_8 yes_no
label values char_9 yes_no
label values char_10 yes_no
label values char_11 yes_no
label def yes_no 0 "No", modify
label def yes_no 1 "Yes", modify