I am having some issue writing my data to excel in a for loop. I am very new to macros and loops.
Basically what I need is a separate excel file for each dependent variable, for each year. Named after the "dependent variable _ year".
This is the code I have:

Code:
local depvar X Y
local Year "2010" "2011"
foreach yr of local Year{

        foreach i of local depvar{
            svy: proportion `i' if Year==`yr'
            matrix `i'=r(table)
            local n=e(N)

            matrix `i'=J(1,9,.)
            matrix colnames `i'="Percentage" "SE" "Lower95%" "Upper95%" "Difference" "SE" "Pvalue" "stars" "Base"
            matrix rownames `i'=`i'
            matrix `i'[1,1]=result[1,2]*100
            matrix `i'[1,2]=result[2,2]*100
            matrix `i'[1,3]=result[5,2]*100
            matrix `i'[1,4]=result[6,2]*100
            matrix `i'[1,9]=`n'
            putexcel set `i'_`yr'.xlsx, replace
            local row=10

* write the column names and the row with just the dependent variable
            putexcel A`row'=matrix(`i'),names
        }        
    }
It runs if I do not loop over the years, and just run it on my data from all years from the same time. But I need this subsample output.
I tihnk the mistake is in this line:
Code:
            putexcel set `i'_`yr'.xlsx, replace
As when I run this do file, I keep getting excel file output named "X_Year.xlsx", but not "X_2010.xlsx".

Does anyone have any ideas?

Oh, and I have to use the putexcel function. Even though using estout and then formatting in Excel later would be easier.