Hello
I have been trying to figure out how to merge multiple tables into one, by adding columns to an existing table in a .txt file using tabout in stata.
I am currently using the .txt files to make tables using tabs, so that I can copy and paste them into excel. I prefer doing it this way rather than export the tables directly into excel. That said, seeing as it's ultimately going into an excel file, it is not a problem to include more columns than would be ideal for a document.
These are the syntaxes I am currently using to make the tables. I have one for my dummy variables and one for my categorical variables.
Dummy:
foreach var of varlist city cityage citygnd citywealth {
tabout one `var' using dummytable_`var'.txt, replace c(mean var_d) sum svy f(2) h1(var_d) h3(nil) npos(row) ptotal(none)
}
Categorical:
foreach var of varlist city cityage citygnd citywealth {
tabout var_c `var' using categoricaltable_`var'.txt, ///
replace c(col) svy f(2) h1(var_c) h3(nil) npos(row) ptotal(none)
}
"var_d" represent a dummy variable and "var_c" represent a categorical variable.
The four variables city, cityage, citygnd and citywealth contain a numerical measure of sample size categorized according to city only, or city and something else.
The variable city holds the sample size of the survey dataset grouped only according to the three cities (lets call them London, Paris and Boston for the sake of simplicity). Thus, the variable city has only three value labels: London, Paris and Boston.
The remaining three variables are categorized according to cities and another relevant grouping.
For instance, the variable cityage holds the sample size grouped according to the three cities, as well as two age groups. Thus, the variable cityage has the value labels: London Age1, London Age2, Paris Age1, Paris Age2, Boston Age1 and Boston Age2.
Only counting the columns containing data, the table I want has 21 columns in total (all the sub groupings/value labels for city cityage citygnd citywealth, summed). As the rows it has the var_d in the dummytable_`var' and var_c in the categoricaltable_`var'.
(If possible, it would be ideal to merge the categorical and dummy tables as well, so that I can have multiple rows with both categorical and dummy variables in the same table, and multiple columns representing the variables city cityage citygnd and citywealth. But this is less critical as having the rows placed further down will be easier to fix later in excel, as long as the columns are placed correctly.)
In the "Tips and Tricks" section of the tabout manual called "Publication quality tables in Stata: a tutorial for the tabout program" (not the manual for Tabout version 3). They describe how to merge several tables into one in a way that seems similar to what I want to achieve. However, these are based on an intent to make a three way table, which is not what I am trying to achieve. Also, the tables in this description are merged by adding rows, instead of columns.
I did find an explanation from a previous post in statalist (Tabout 3 summary statistics transposing columns to rows - Statalist) for how to turn rows into multiple columns, but from what I understand this description is meant for one variable with multiple value labels, and not multiple variables with each their own set of value labels.
(I was however able to use this description to make the dummytable_`var' horizontal instead of vertical, so that was helpful
)
Any help would be highly appreciated!
Best,
Maria Chavez

I have been trying to figure out how to merge multiple tables into one, by adding columns to an existing table in a .txt file using tabout in stata.
I am currently using the .txt files to make tables using tabs, so that I can copy and paste them into excel. I prefer doing it this way rather than export the tables directly into excel. That said, seeing as it's ultimately going into an excel file, it is not a problem to include more columns than would be ideal for a document.
These are the syntaxes I am currently using to make the tables. I have one for my dummy variables and one for my categorical variables.
Dummy:
foreach var of varlist city cityage citygnd citywealth {
tabout one `var' using dummytable_`var'.txt, replace c(mean var_d) sum svy f(2) h1(var_d) h3(nil) npos(row) ptotal(none)
}
Categorical:
foreach var of varlist city cityage citygnd citywealth {
tabout var_c `var' using categoricaltable_`var'.txt, ///
replace c(col) svy f(2) h1(var_c) h3(nil) npos(row) ptotal(none)
}
"var_d" represent a dummy variable and "var_c" represent a categorical variable.
The four variables city, cityage, citygnd and citywealth contain a numerical measure of sample size categorized according to city only, or city and something else.
The variable city holds the sample size of the survey dataset grouped only according to the three cities (lets call them London, Paris and Boston for the sake of simplicity). Thus, the variable city has only three value labels: London, Paris and Boston.
The remaining three variables are categorized according to cities and another relevant grouping.
For instance, the variable cityage holds the sample size grouped according to the three cities, as well as two age groups. Thus, the variable cityage has the value labels: London Age1, London Age2, Paris Age1, Paris Age2, Boston Age1 and Boston Age2.
Only counting the columns containing data, the table I want has 21 columns in total (all the sub groupings/value labels for city cityage citygnd citywealth, summed). As the rows it has the var_d in the dummytable_`var' and var_c in the categoricaltable_`var'.
(If possible, it would be ideal to merge the categorical and dummy tables as well, so that I can have multiple rows with both categorical and dummy variables in the same table, and multiple columns representing the variables city cityage citygnd and citywealth. But this is less critical as having the rows placed further down will be easier to fix later in excel, as long as the columns are placed correctly.)
In the "Tips and Tricks" section of the tabout manual called "Publication quality tables in Stata: a tutorial for the tabout program" (not the manual for Tabout version 3). They describe how to merge several tables into one in a way that seems similar to what I want to achieve. However, these are based on an intent to make a three way table, which is not what I am trying to achieve. Also, the tables in this description are merged by adding rows, instead of columns.
I did find an explanation from a previous post in statalist (Tabout 3 summary statistics transposing columns to rows - Statalist) for how to turn rows into multiple columns, but from what I understand this description is meant for one variable with multiple value labels, and not multiple variables with each their own set of value labels.
(I was however able to use this description to make the dummytable_`var' horizontal instead of vertical, so that was helpful

Any help would be highly appreciated!
Best,
Maria Chavez
Comment