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
Merge Panel Data set by IdsI have a panel data set (Master) with id, name, month, and year. I have another data set (Data set …
help with foreach using 2 variable lists to generate a new variableHello all. I am trying to use foreach to generate a new variable based on multiple variables from 2 …
different count problems within variablesHi there! The forum already helped me a great deal (thx especially to Nick Cox!). However I am now …
Can I take log and lag on my independent variables in a regression?Hello, Please can someone answer me if it is possible to take log and lag (both at the same time) on…
Stata compatabilityApologies for what is probably a very basic question - am new user! I have started doing quite a lot…
Subscribe to:
Post Comments (Atom)
0 Response to Matrix - loop across variables
Post a Comment