Hi Stata listers
I am trying to work with local and perhaps global macros in do-files.
The code below works when it is run in its entirety. If you put the code nominated (marked by ******) in a separate do file and call it by:
do "dofilename.do"
Nothing happens. I tried replacing local with global for the macros (indv, strv etc) and this also didn't work.
Almost certain it is the local and global issue but not sure how to fix.
Can anyone point me in the right direction?
Thanks very much.
Darren
__________________________________________________ __________________________________________________ __________________________________
use "Q:\STATA\Surveys\2013-14\Adult\data_pooling_process\Data\SRHS2013_14_MAS TER_TEMP.dta", clear
svyset [pweight= pwt_18plus_FINAL]
gen dumif=1
local indv daily_smoke
local strv persons rsex agecat_nhs
local if1 edu_sum marri_sum
local if2 dumif
local geov lga2014_FINAL
************************************************** **********************************
//CONTENT FOR A SEPARATE DO FILE BELOW
************************************************** **********************************
mata
cmndf=""
colf=("","","")
estf=(.,.,.,.,.,.)
end
foreach ifs1 in `if1' {
levelsof `ifs1', local(ifl1)
foreach l1 of local ifl1 {
foreach ifs2 in `if2' {
levelsof `ifs2', local(ifl2)
foreach l2 of local ifl2 {
foreach geo in `geov' {
levelsof `geo', local(geog)
foreach g of local geog {
foreach ind in `indv' {
foreach str in `strv' {
capture estpost svy: tab `str' `ind' if `ifs1'==`l1' & `ifs2'==`l2' & `geo'==`g', row
if _rc==0 {
estpost svy: tab `str' `ind' if `ifs1'==`l1' & `ifs2'==`l2' & `geo'==`g', row notot
mat t=e(row)\e(se)\e(lb)\e(ub)\e(obs)\e(count)
mat t=t'
local cmnd=e(cmdline)
local wtvar=e(wvar)
mata: cmd=st_local("cmnd")
mata: wtv=st_local("wtvar")
mata: cmnd = J(cols(st_matrix("e(b)")),1,cmd)
mata: wtvar =J(cols(st_matrix("e(b)")),1,wtv)
mata: est=st_matrix("t")
mata: col=st_matrixcolstripe("e(b)")
mata: col=col,wtvar
mata: colf=colf\col
mata: estf=estf\est
mata: cmndf=cmndf\cmnd
}
}
}
}
}
}
}
}
}
************************************************** ******************************************
************************************************** ******************************************
I am trying to work with local and perhaps global macros in do-files.
The code below works when it is run in its entirety. If you put the code nominated (marked by ******) in a separate do file and call it by:
do "dofilename.do"
Nothing happens. I tried replacing local with global for the macros (indv, strv etc) and this also didn't work.
Almost certain it is the local and global issue but not sure how to fix.
Can anyone point me in the right direction?
Thanks very much.
Darren
__________________________________________________ __________________________________________________ __________________________________
use "Q:\STATA\Surveys\2013-14\Adult\data_pooling_process\Data\SRHS2013_14_MAS TER_TEMP.dta", clear
svyset [pweight= pwt_18plus_FINAL]
gen dumif=1
local indv daily_smoke
local strv persons rsex agecat_nhs
local if1 edu_sum marri_sum
local if2 dumif
local geov lga2014_FINAL
************************************************** **********************************
//CONTENT FOR A SEPARATE DO FILE BELOW
************************************************** **********************************
mata
cmndf=""
colf=("","","")
estf=(.,.,.,.,.,.)
end
foreach ifs1 in `if1' {
levelsof `ifs1', local(ifl1)
foreach l1 of local ifl1 {
foreach ifs2 in `if2' {
levelsof `ifs2', local(ifl2)
foreach l2 of local ifl2 {
foreach geo in `geov' {
levelsof `geo', local(geog)
foreach g of local geog {
foreach ind in `indv' {
foreach str in `strv' {
capture estpost svy: tab `str' `ind' if `ifs1'==`l1' & `ifs2'==`l2' & `geo'==`g', row
if _rc==0 {
estpost svy: tab `str' `ind' if `ifs1'==`l1' & `ifs2'==`l2' & `geo'==`g', row notot
mat t=e(row)\e(se)\e(lb)\e(ub)\e(obs)\e(count)
mat t=t'
local cmnd=e(cmdline)
local wtvar=e(wvar)
mata: cmd=st_local("cmnd")
mata: wtv=st_local("wtvar")
mata: cmnd = J(cols(st_matrix("e(b)")),1,cmd)
mata: wtvar =J(cols(st_matrix("e(b)")),1,wtv)
mata: est=st_matrix("t")
mata: col=st_matrixcolstripe("e(b)")
mata: col=col,wtvar
mata: colf=colf\col
mata: estf=estf\est
mata: cmndf=cmndf\cmnd
}
}
}
}
}
}
}
}
}
************************************************** ******************************************
************************************************** ******************************************
Comment