Hello

I am trying to figure out how to make a table with one variable as the rows, and several variables nested as the columns.
Attached is an illustration of the table I am attempting to make. In the displayed table I filled out some of the cells manually just to illustrate, based on the information from more simple tables I was able to make in stata .
As I have previously received help from you for presenting multiple columns and saving it as a .txt file (previous thread: Merging tables horizontally(multiple columns) as .txt files in stata, using tabout. In order to copy-paste to excel. - Statalist), I have attempted to build on this code for the current conundrum as well.
Unlike last time, and as displayed in the picture attached , I now only have a need for one variable as my rows. In addition to this, I want my column variables presented as nested where the total, and the variables collgrad, married and race are presented repeatedly, for each of the industry values/categories.
This is my latest attempt:
ssc install estout, clear
webuse nlsw88, clear
estimates drop _all
local i 0
local vars "occupation"
foreach var1 of local vars{
foreach var2 of varlist collgrad married race{
local ++i
eststo m`i': estpost tab `var1' `var2'
}
local opt= cond("`var1'"==word("`vars'", 1), "replace nonumb", "append eqlab(none) nonumb collab(none)")
esttab m* using myfile3.txt, cells(colpct(fmt(2))) unstack drop(Total: Total) `opt'
estimates drop _all
}
However, I was not able to figure out how to achieve the nested columns where the variables collgrad, married and race are repeated for each category of industry. Nor was I able to add a column for the total.
Can you help me with this, please?
Like last time, I would also prefer that the output is placed in a .txt file, so that I can copy paste into an excel file
My actual dataset that I am trying to make the tables from is based on a survey that is weighted with the variable "weightcorr_n". In this dataset all "Refuse to answer" and "I don't know" were registered as missing data. That said, we want to include these in the table for the total to be accurate. With a simple tab function I know that I can fix this by adding "missing", for instance like so:
tab occupation race if industry==1 [aw = weightcorr_n], col missing nofreq
I am using nlsw88 as my example dataset as it was the only example dataset I could find with enough variables that also had subcategories in order to achieve the multiple and nested columns. However, due to it not being survey data, and without data that is registered as missing, I am not sure how to add these commands to the code for the example dataset in a meaningful way.
Hence, I would be very grateful if you have any pointers as to how I could adapt your suggested codes to a survey dataset, including the weighting command and also a way to add the missing data.
Best,
Maria
Comment