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
Choice of Estimator for Dynamic Panel AnalysisHi, I am working on determinants of corporate cash holdings with a panel dataset of ~700 firms acr…
Export Summary Statistics of a Loop to Word FileDear Statalisters, I am interested in the summary statistics (specifically, the number of observati…
r(2000) no observations? I have tried 'destring', but then it says 'already numeric; no generateHello I am trying to make a regression using mlogit. But once I enter a specific one of my variables…
create loopHello, I am new to stata and I can't figure out how to use loop (if it is the case) for the followi…
COURSE using Stata: Causal Inference in Observational Studies and Clinical Trials Affected by Treatment SwitchingCausal Inference in Observational Studies and Clinical Trials Affected by Treatment Switching: A Pra…
Subscribe to:
Post Comments (Atom)
0 Response to Matrix - loop across variables
Post a Comment