I am using Windows 7, 64-bit, and Stata 14.2 and I am aiming to send crosstabulation of one string variable and value labels of its numeric version to excel using the following code:
local root "X/metadata/study1/"
cap mkdir `root'/mapping
local putexcel `root'/mapping/crs_bfup.xlsx
putexcel set `putexcel', sheet(start) replace
rename hdoc hdoc_t
gen hdoc=.
replace hdoc = 1 if hdoc_t == "P"
replace hdoc = 2 if hdoc_t == "C"
replace hdoc = 3 if hdoc_t == "M"
replace hdoc = 4 if hdoc_t == "T"
replace hdoc = 5 if hdoc_t == "N"
label define hdoc 1 "Child Health Passport", modify
label define hdoc 2 "Child Health Card (green print)", modify
label define hdoc 3 "Missing(P or C is there but temp. not available)", modify
label define hdoc 4 "Temporary record(eg piece of paper)", modify
label define hdoc 5 "None (never issued or lost for good)", modify
label define hdoc 8 "Unknown", modify
label define hdoc 9 "Missing", modify
label val hdoc hdoc
//creating a crosstab of the original string and the new numeric variable and
//sending the crosstab to excel
putexcel set `putexcel', sheet(hdoc) modify
decode hdoc, gen(hdoc_s)
tabulate hdoc_s hdoc_t, m matcell(cellcounts)
levelsof hdoc_s, miss local(hdoc_labels)
levelsof hdoc_t, miss local(hdoc_t_values)
matrix rownames cellcounts = `hdoc_labels'
matrix colnames cellcounts = `hdoc_t_values'
putexcel A1 = matrix(cellcounts), names hcenter
drop hdoc_t hdoc_s
it seems to be working fine until get to matrix rownames cellcounts = `hdoc_labels'
where I get the following error
None (never issued or lost for good) invalid name
I basically adapted the code from http://blog.stata.com/2017/01/24/cre...a-expressions/
Example 8
I want ot do this for a lot of varibles...
Your help will be greatly appreciated.
Thanks!
local root "X/metadata/study1/"
cap mkdir `root'/mapping
local putexcel `root'/mapping/crs_bfup.xlsx
putexcel set `putexcel', sheet(start) replace
rename hdoc hdoc_t
gen hdoc=.
replace hdoc = 1 if hdoc_t == "P"
replace hdoc = 2 if hdoc_t == "C"
replace hdoc = 3 if hdoc_t == "M"
replace hdoc = 4 if hdoc_t == "T"
replace hdoc = 5 if hdoc_t == "N"
label define hdoc 1 "Child Health Passport", modify
label define hdoc 2 "Child Health Card (green print)", modify
label define hdoc 3 "Missing(P or C is there but temp. not available)", modify
label define hdoc 4 "Temporary record(eg piece of paper)", modify
label define hdoc 5 "None (never issued or lost for good)", modify
label define hdoc 8 "Unknown", modify
label define hdoc 9 "Missing", modify
label val hdoc hdoc
//creating a crosstab of the original string and the new numeric variable and
//sending the crosstab to excel
putexcel set `putexcel', sheet(hdoc) modify
decode hdoc, gen(hdoc_s)
tabulate hdoc_s hdoc_t, m matcell(cellcounts)
levelsof hdoc_s, miss local(hdoc_labels)
levelsof hdoc_t, miss local(hdoc_t_values)
matrix rownames cellcounts = `hdoc_labels'
matrix colnames cellcounts = `hdoc_t_values'
putexcel A1 = matrix(cellcounts), names hcenter
drop hdoc_t hdoc_s
it seems to be working fine until get to matrix rownames cellcounts = `hdoc_labels'
where I get the following error
None (never issued or lost for good) invalid name
I basically adapted the code from http://blog.stata.com/2017/01/24/cre...a-expressions/
Example 8
I want ot do this for a lot of varibles...
Your help will be greatly appreciated.
Thanks!
Comment