Hi there,

I wonder if anybody could help. This might be very obvious to more experienced users so I'll pop this here for now but I will have to create a dummy dataset for an example if required (I'm using sensitive data). I'm trying to get the proportions of a set of variables over a number of dates and then export them to an excel file. The code below works for one variable (var1) but when I add the second (var2), it produces the error "r(101) var2 not allowed". I have a feeling I'm doing something that is very obviously wrong but I can't quite work it out.

This is what it produces with just one variable (var1):
% 95%CI- 95%CI+
var1_variable_label : July x x x
var1_variable_label : August x x x
var1_variable_label : September x x x
...

code:

local var "date"
local dvar "var1 var2"

foreach v of varlist `dvar'{
local var_lab: variable label `dvar'
foreach vv of varlist `var'{
quiet levelsof `var', local(levels)
local value_lab: value label `var'
foreach i of local levels{
local lb `i' label `value_lab' `i'
local tmp `""`var_lab':`lb`i' ' " " '
local m_rown `"`m_rown'`tmp' " '
}

proportion `dvar', over(`var') percent
mat t = r(table)'
mat t = t[t[12...,1..1], t[12...,5..6]]
mat rownames t = `m_rown'
mat R = [., ., .]
mat colnames R = "%" "95% CI-" "95% CI+"
mat R = R/t
}
}
mat R = R[2..., 1...]
mat list R

quiet putexcel clear
quiet putexcel set "C:\x\x\x\x\x\results.xlsx", modify sheet("proportion", replace)
quiet putexcel (A1) = mat(R), names overwritefmt


If anybody could help I would be very grateful,

Kind Regards,
Hannah