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
Related Posts with Matrix - loop across variables
Subgroups with coefplot allowing each graph to have its own scale and adjust location of legendDear all I am using the following code to create a graph with 5 plots: Code: coefplot (hba1c1, …
insheeting excel files using loop and extracting filename as variable valueHello, I am trying to use insheet multiple .xlxs files and then extract filename. This is my code: …
Spatial Panel Data TestsHello, I am relatively new to Stata and have been estimating different spatial panels, such as the …
Conditional drop if command within a certain firm year levelHi, I am working on my master thesis and I am currently busy with the data cleaning process. One of…
Interacting firm fixed effects for numerous firms with a continuous variable - Execution and interpretationDear all, I am attempting to replicate a series of OLS regressions with respect to the effect of CE…
Subscribe to:
Post Comments (Atom)
0 Response to Matrix - loop across variables
Post a Comment