Announcement

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

  • putexcel column % with svy

    I am hoping someone could help with modifying the following code below. I would like the col % not row% and I want it to only tab if the conditions are met. I can't seem to make the table populate similar to the outputs with the svy command and don't know how to specify svy in the putexcel code . How can I also specify the weighted vs unweighted counts. The proportions will be based off the weighted counts. Thank you!

    Code:
    svy: tabulate a4_1_obst pop1_low_edu if a4_1_obst==1 |a4_1_obst==0, obs count col format(%11.3g)
    
    
    local RowVar = "a4_1_obst"
    local ColVar = "pop1_low_edu"
    tabulate `RowVar' if !missing(`ColVar') , matcell(rowtotals)
    tabulate `ColVar' if !missing(`RowVar'), matcell(coltotals)
    tabulate `RowVar' `ColVar', matcell(cellcounts)
    local RowCount = r(r)
    local ColCount = r(c)
    local TotalCount = r(N)
     
    levelsof `RowVar', local(RowLevels)
    local RowValueLabel : value label `RowVar'
     
    levelsof `ColVar', local(ColLevels)
    local ColValueLabel : value label `ColVar'
     
    putexcel set example.xlsx, sheet(example1) modify
    forvalues row = 1/`RowCount' {
     
        local RowValueLabelNum = word("`RowLevels'", `row')
        local CellContents : label `RowValueLabel' `RowValueLabelNum'
        local Cell = char(64 + 1) + string(`row'+1)
        putexcel `Cell' = "`CellContents'", right
            
        local CellContents = rowtotals[`row',1]
        local Cell = char(64 + `ColCount' + 2) + string(`row' + 1)
        putexcel `Cell' = "`CellContents'", hcenter
     
        forvalues col = 1/`ColCount' {
            local cellcount = cellcounts[`row',`col']
            local cellpercent = string(100*`cellcount'/rowtotals[`row',1],"%9.1f")
            local CellContents = "`cellcount' (`cellpercent'%)"
            local Cell = char(64 + `col' + 1) + string(`row' + 1)
            putexcel `Cell' = "`CellContents'", right
            
            if `row'==1 {
                    local ColValueLabelNum = word("`ColLevels'", `col')
                    local CellContents : label `ColValueLabel' `ColValueLabelNum'
                    local Cell = char(64 + `col' + 1) + string(1)
                    putexcel `Cell' = "`CellContents'", hcenter
                            
                    local CellContents = coltotals[`col',1]
                    local Cell = char(64 + `col' + 1) + string(`RowCount' + 2)
                    putexcel `Cell' = "`CellContents'", hcenter
            }
        }
    }
     
    local Cell = char(64 + `ColCount' + 2) + string(`RowCount' + 2)
    putexcel `Cell' = "`TotalCount'", hcenter
     
    local Cell = char(64 + `ColCount' + 2) + string(1)
    putexcel `Cell' = "Total", hcenter
     
    local Cell = char(64 + 1) + string(`RowCount' + 2)
    putexcel `Cell' = "Total", right
     
    local UpperLeft = char(64 + 1)+ string(1)
    local UpperRight = char(64 + `ColCount' + 2)+ string(1)
    local BottomLeft = char(64 + 1)+ string(`RowCount'+2)
    local BottomRight = char(64 + `ColCount' + 2)+ string(`RowCount'+2)
     
    local CellRange =  "`UpperLeft':`UpperRight'"
    putexcel `CellRange', border(bottom)
     
    local CellRange =  "`BottomLeft':`BottomRight'"
    putexcel `CellRange', border(top)
     
    local CellRange =  "`UpperLeft':`BottomLeft'"
    putexcel `CellRange', border(right)
     
    local CellRange =  "`UpperRight':`BottomRight'"
    putexcel `CellRange', border(left)
    Hoping to get an output in excel like this.
    Click image for larger version

Name:	stata example.PNG
Views:	1
Size:	4.5 KB
ID:	1721176

    Last edited by Tia Landry; 19 Jul 2023, 14:06.

  • #2
    I should mention that I would like my other results run to either be stacked below.

    Comment

    Working...
    X