Monday, September 28, 2020

putexcel with dates as row names

Dear All,

I'm trying to compute some statistics of my sample dataset. In particular I'm interested in using putexcel with dates as row names. I found an interesting guide at this link https://blog.stata.com/2017/04/06/cr...ary-variables/ and tried to replicate but it does not work. Any idea how can i bypass this issue? Thanks.

Code:
clear
input date flag
718 1
718 0
718 0
718 0
718 0

719 1
719 1
719 0
719 0

720 1
720 0
720 0

end

list

format date %tm

tabulate date flag

tabulate date flag, matcell(cellcounts)
matrix list cellcounts

local RowVar = "date"
local ColVar = "flag"
tabulate `RowVar' if !missing(`ColVar'), matcell(rowtotals)
tabulate `RowVar' `ColVar', matcell(cellcounts)
local RowCount = r(r)
local ColCount = r(c)
 
local RowValueLabel : value label `RowVar'
levelsof `RowVar', local(RowLevels)
 
putexcel set putexcel3.xlsx, sheet(example7) 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
 
    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
    }
}

No comments:

Post a Comment