Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Open Parenthesis/Bracket - Issue with Finding

    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'"
    }

  • #2
    I can't find the problem either. But this code won't fly from zero. What precedes it?

    FWIW:

    Some of the things you can do in this territory

    * indent code to show structure

    * comment each loop

    * check for matching ( [ { in a decent text editor

    * count characters (e.g. using chartab from SSC).


    I got as far as this:

    Code:
    foreach x in Q41 Q51 Q43 Q44 { // loop 1 
    
        local j=0
    
        foreach y in dummy cohort Q2 race Q4 { // loop 2 
    
            local i=0
    
            foreach var of varlist `x'* { // loop 3 
    
                putexcel set TA_ALL_tabs_p2.xlsx, sheet(`x') modify
    
                *putexcel A1="`question_Q`x''"
    
                // creates rowtotals
                tabulate `y' if !missing(`var'), matcell(rowtotals) 
    
                tabulate `var' if !missing(`y'), matcell(coltotals)
    
                // creates cellcounts
                tab `y' `var', matcell(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
    
                // this is one loop, can cut
                forvalues row = 1/`RowCount' { // loop 4  
    
                    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' { // loop 5 
                        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 col loop 5 
    
                    forvalues row = 1/`RowCount' { // loop 6
    
                        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' {  // loop 7 
                            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'"
                            }
                        } // forvalues col loop 7  
    
                    } // forvalues row loop 6  
    
                } // forvalues row loop 4  
    
                local i=`i'+1
    
            } // foreach var loop 3  
    
        local j=`j'+`RowCount'+2
        } // foreach y loop 2  
    
        putexcel A`j' = "n"
        local rowtotal=rowtotal[1,1]
        putexcel B`j'= "`rowtotal'"
    } // foreach x loop 1

    Comment

    Working...
    X