I am trying to export tables of about a dozen variables into excel using putexcel. My codes are as follows:
local colvar="sex"
local ColValueLabel: value label `colvar'
levelsof `colvar', local(colevels)
local ColValueLabelNum = word("`colevels'",1)
local CoLabel: label `ColValueLabel' `ColValueLabelNum'
display "`ColValueLabel' `colevels' `CoLabel'"
foreach RowVars of varlist nation18 usedent educat jobcat18 {
tab `colvar' if index==1, matcell(coltotals)
tab `RowVars' `colvar' if index==1, matcell(cellcounts)
local Totalcount=r(N)
local Rowcount=r(r)
local Colcount=r(c)
putexcel set "V:\Baselines.xlsx", sheet(1_Baselinetables) modify
forvalues row=1/`Rowcount' {
local RowValueLabel: value label `RowVars'
display "`RowValueLabel'"
levelsof `RowVars', local(RowLevels)
local RowValueLabelNum = word("`Rowlevels'",`row')
local RowLabel: label `RowValueLabel' `RowValueLabelNum'
di "`RowValueLabel' `Rowlevels' `RowLabel'"
local Cellcontents: label `RowValueLabel' `RowValueLabelNum'
local cell = char(65+1) + string(`row'+1)
putexcel `cell' = `Cellcontents', right bold
local contents = coltotals[`row',1]
local cell = char(65+`colcount') + string(`row"+1)
putexcel `cell' = `contents', right
forvalues col=1/`Colcount'{
local cellcount=cellcounts[`row',`col']
local cellpercent = string(100*`cellcount'/`coltotals'[`col',1], %9.1f)
local Cellcontents = `cellcount' "("cellpercent'"%)"
di "cellcounts[`row',`col']=`cellcount'"
local cell = char(64+`col') + `row'
putexcel `cell'=`cellcontents', right
}
}
}
---end of do-file
The first part seems to work for colvar "sex". But when Stata executes line: < local RowLabel: label `RowValueLabel' `RowValueLabelNum' >, I get this: <local RowLabel: label nation > followed by an error message on the next line: "invalid syntax".
Can someone tell me what am I writing wrong in this? For clarification, this is an example of the table I would like to produce in Excel:
Men (N, %) | Women (N, %) | P-value | |
Nationality | |||
Education | |||
Educ_lvl1 | |||
Educ_lvl2 | |||
Educ_lvl3 | |||
Occupation | |||
Occup_1 | |||
Occup_2 | |||
Occup_3 |

Thanks!
0 Response to "invalid syntax" error when saving variable label for putexcel
Post a Comment