Hello!
I am trying to fix a piece of code from an analysis file that I did not write, and having a few issues. When I run this, it throws the error "invalid something: unmatched open parenthesis or bracket". I have the same number, so I assuming one is in the wrong place, or some other formatting issue is the problem. Does anyone have any suggestions?
foreach x in Q41 Q51 Q43 Q44 {
local j=0
foreach y in dummy cohort Q2 race Q4 {
local i=0
foreach var of varlist `x'* {
putexcel set TA_ALL_tabs_p2.xlsx, sheet(`x') modify
*putexcel A1="`question_Q`x''"
tabulate `y' if !missing(`var'), matcell(rowtotals) //creates rowtotals
tabulate `var' if !missing(`y'), matcell(coltotals)
tab `y' `var', matcell(cellcounts) //creates cellcounts
local RowCount = r(r)
local ColCount = r(c)
local RowValueLabel : value label `y' //this should be the q name
levelsof `y', local(RowLevels)
local ColValueLabel : value label `var'
levelsof `var', local(ColLevels)
tab `var', matcell(total)
matrix rowtotal = J(1, rowsof(total), 1) * total //cut?
mat list rowtotal
forvalues row = 1/`RowCount' { //this is one loop, can cut
if `i'<1 { //means this is the first row
local RowValueLabelNum = word("`RowLevels'", `row')
local CellContents : label `RowValueLabel' `RowValueLabelNum'
local Cell = char(64 + 1) + string(`row'+1 + `j')
putexcel `Cell' = "`CellContents'", right
}
forvalues col = 2/`ColCount' {
local cellcount = cellcounts[`row',`col']
local cellpercent = string(100*`cellcount'/rowtotals[`row',1],"%9.1f")
local CellContents = "`cellpercent'%"
local Cell = char(64 + 2 + `i') + string(`row' + 1 +`j')
putexcel `Cell' = "`CellContents'", right
if `row'==1 {
local ColValueLabelNum = word("`ColLevels'", `col')
local CellContents : var l `var'
local Cell = char(64 + `col' + `i' ) + string(1)
putexcel `Cell' = "`CellContents'"
}
}
forvalues row = 1/`RowCount' {
if `i'<1 { //means this is the first row
local RowValueLabelNum = word("`RowLevels'", `row')
local CellContents : label `RowValueLabel' `RowValueLabelNum'
local Cell = char(64 + 6) + string(`row'+1 + `j')
putexcel `Cell' = "`CellContents'", right
}
forvalues col = 2/`ColCount' {
local cellcount = cellcounts[`row',`col']
local cellpercent = string(100*`cellcount'/rowtotals[`row',1],"%9.1f") //originally removed
local CellContents = "`cellpercent'%" //originally removed
local Cell = char(64 + 6 + `i') + string(`row' + 1 +`j')
putexcel `Cell' = "`cellcount'", right
if `row'==1 {
local ColValueLabelNum = word("`ColLevels'", `col')
local CellContents : var l `var'
local Cell = char(64 + `col' + `i' +4) + string(1)
putexcel `Cell' = "`CellContents'"
}
}
}
}
local i=`i'+1
}
local j=`j'+`RowCount'+2
}
putexcel A`j' = "n"
local rowtotal=rowtotal[1,1]
putexcel B`j'= "`rowtotal'"
}
I am trying to fix a piece of code from an analysis file that I did not write, and having a few issues. When I run this, it throws the error "invalid something: unmatched open parenthesis or bracket". I have the same number, so I assuming one is in the wrong place, or some other formatting issue is the problem. Does anyone have any suggestions?
foreach x in Q41 Q51 Q43 Q44 {
local j=0
foreach y in dummy cohort Q2 race Q4 {
local i=0
foreach var of varlist `x'* {
putexcel set TA_ALL_tabs_p2.xlsx, sheet(`x') modify
*putexcel A1="`question_Q`x''"
tabulate `y' if !missing(`var'), matcell(rowtotals) //creates rowtotals
tabulate `var' if !missing(`y'), matcell(coltotals)
tab `y' `var', matcell(cellcounts) //creates cellcounts
local RowCount = r(r)
local ColCount = r(c)
local RowValueLabel : value label `y' //this should be the q name
levelsof `y', local(RowLevels)
local ColValueLabel : value label `var'
levelsof `var', local(ColLevels)
tab `var', matcell(total)
matrix rowtotal = J(1, rowsof(total), 1) * total //cut?
mat list rowtotal
forvalues row = 1/`RowCount' { //this is one loop, can cut
if `i'<1 { //means this is the first row
local RowValueLabelNum = word("`RowLevels'", `row')
local CellContents : label `RowValueLabel' `RowValueLabelNum'
local Cell = char(64 + 1) + string(`row'+1 + `j')
putexcel `Cell' = "`CellContents'", right
}
forvalues col = 2/`ColCount' {
local cellcount = cellcounts[`row',`col']
local cellpercent = string(100*`cellcount'/rowtotals[`row',1],"%9.1f")
local CellContents = "`cellpercent'%"
local Cell = char(64 + 2 + `i') + string(`row' + 1 +`j')
putexcel `Cell' = "`CellContents'", right
if `row'==1 {
local ColValueLabelNum = word("`ColLevels'", `col')
local CellContents : var l `var'
local Cell = char(64 + `col' + `i' ) + string(1)
putexcel `Cell' = "`CellContents'"
}
}
forvalues row = 1/`RowCount' {
if `i'<1 { //means this is the first row
local RowValueLabelNum = word("`RowLevels'", `row')
local CellContents : label `RowValueLabel' `RowValueLabelNum'
local Cell = char(64 + 6) + string(`row'+1 + `j')
putexcel `Cell' = "`CellContents'", right
}
forvalues col = 2/`ColCount' {
local cellcount = cellcounts[`row',`col']
local cellpercent = string(100*`cellcount'/rowtotals[`row',1],"%9.1f") //originally removed
local CellContents = "`cellpercent'%" //originally removed
local Cell = char(64 + 6 + `i') + string(`row' + 1 +`j')
putexcel `Cell' = "`cellcount'", right
if `row'==1 {
local ColValueLabelNum = word("`ColLevels'", `col')
local CellContents : var l `var'
local Cell = char(64 + `col' + `i' +4) + string(1)
putexcel `Cell' = "`CellContents'"
}
}
}
}
local i=`i'+1
}
local j=`j'+`RowCount'+2
}
putexcel A`j' = "n"
local rowtotal=rowtotal[1,1]
putexcel B`j'= "`rowtotal'"
}
Comment