When working with large datasets, creating & exporting summary/descriptive tables can be a pain, even if you take advantage of looping over the variables in combination with table exporting programs like -tabout- . The reason is that you want different kinds of summary tables for different kinds of variables. For categorical string variables you probably want a cross-tab. Creating tables for numeric variables is less straightforward. For continuos numeric variables with many categories, you probably want a table with the mean, sd, min, max, and so on. For numeric variables that are categorical or otherwise limited, you may want a cross-tab.
So, while you could use -ds- to detect the "type" (e.g. string or numeric) of variable you have, this could be misleading if a numeric categorical variable has many values (say 50+). Another approach could be to decide the type of table you create & export based on the number of unique values of a categorical numeric variable. Here we could use the -egenmore- (from SSC) function nvals() after detecting the variable type using -ds-.
Here's an example where the variables with more than 50 unique categories are treated as continuous variables:
So, while you could use -ds- to detect the "type" (e.g. string or numeric) of variable you have, this could be misleading if a numeric categorical variable has many values (say 50+). Another approach could be to decide the type of table you create & export based on the number of unique values of a categorical numeric variable. Here we could use the -egenmore- (from SSC) function nvals() after detecting the variable type using -ds-.
Here's an example where the variables with more than 50 unique categories are treated as continuous variables:
*-------------------------------------------------BEGIN EXAMPLE
global files "yourfilehere" ** from web-aware Stata: ssc install fre ssc install tabout ssc install egenmore ** webuse auto, clear ds, has(type numeric) local all `r(varlist)' di "`all'" foreach v in `all' { egen zz = nvals(`v') if zz > 50 & !mi(zz) { di "----------------------------------" di " Descriptives for variable: `v' " table for, c(n `v' mean `v' sd `v' min `v' max `v' ) col di "totals-->" sum `v' qui { tabout for using "$files\descriptives_`v'.txt", /* */ format(3) sum replace c(mean `v' sd `v' min `v' max `v') /* */ npos(lab) nlab((n=#)) h1(title here) } } else { di "----------------------------------" di " Descriptives for c. variable: `v' " fre `v' , nowrap des t(10) fre `v' using "$files\onewaydescriptives_`v'.txt" , /*
*/ all nowrap des replace tab2 `v' for, col chi2 miss qui { tabout `v' for using "$files\descriptives_`v'.txt", /* */ replace c( col) f(2p) npos(lab) nlab( (n = #) ) /* */ ptotal(all) h1(title here) } di "~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~" } cap drop zz } ds, has(type string) local all `r(varlist)' di "`all'" foreach v in `all' { di "----------------------------------" di " Descriptives for c. variable: `v' " fre `v' , nowrap des t(10) fre `v' using "$files\onewaydescriptives_`v'.txt" , /* */ all nowrap des replace tab2 `v' for, col chi2 miss qui { tabout `v' for using "$files\descriptives_`v'.txt", /* */ replace c( col) f(2p) npos(lab) nlab( (n = #) ) /* */ ptotal(all) h1(title here) } di "~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~" }
*-------------------------------------------------END EXAMPLE
Comments
Post a Comment