Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: st: Creating a table with list of variables, but showing all values even if value is not used


From   Roy Wada <roywada@hotmail.com>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: Creating a table with list of variables, but showing all values even if value is not used
Date   Mon, 18 May 2009 01:04:52 -0700

 
> I would like to create a table with 30 columns and 12 rows, containing
> the number of people and percentages in each category (row) at each
> age (column). My current strategy is to tabulate each of the 30
> variables separately and copy and paste the data into excel. (tab1
> activity*).
> Obviously if at some age no person is engaged in one of the 12
> categories (e.g at 15 no one is employed full-time), this category is
> not listed in the tabulation and it makes it very time consuming to
> manually make sure the rows match in excel, when each of the tabs of
> the variables has a different number of categories. Is there an easy
> way to 'force' tab to show all the 12 categories but to have either a
> missing or a zero to indicate that no one was in that category.
> 
> Anna
 
What Anna asked for is a single table of multiple one-way tabluations.
 
A relative simple solution is to use -logout- to import the information 
created by -tab- and -merge- them together, and export them to Excel.
 
I wrote a wrapper that will do this. I have sent it off to Kit Baum 
to be placed on ssc. The example would be this:
 
    sysuse auto, clear
    gen activity1=mpg
    gen activity2=rep78+mpg
    gen activity3=mpg-rep78+3
    keep activity*
    tabone activity* using myfile, tex excel replace

tabone will install -dataout- and -logout- if not already installed. 
(it will not double-install).
 
User-specified -sort- option would be nice, but no one asked for it.
 
Good thing about this programming strategy is the low cost of programming. 
It's dirt cheap compared to doing it from scratch. With a little modification, 
you can churn out all sorts of table-making programs.
 
Roy
 
 
 
 
*! version 1.0.0 18May2009 tabone by roywada@hotmail.com
*! export single or multiple tabulations into various output formats
prog define tabone
version 7
syntax varlist(min=1) using/, [excel word tex replace auto(numlist max=1) NOAUTO dec(numlist max=1)]
* checking the existence
cap which dataout.ado
if _rc~=0 {
 ssc install dataout, replace
}
cap which logout.ado
if _rc~=0 {
 ssc install logout, replace
}
preserve
tempfile mydata
qui save `mydata', replace
foreach var of local varlist {
 qui use `mydata', clear
 cap log close
 logout, clear : tab `var'
 qui drop in 1
 qui sort t1
 qui drop t4
 qui ren t2 `var'
 qui ren t3 `var'594852963
 * check for label stuff
 if `var'[2]=="Freq." {
  qui drop in 1/2
 }
 if `var'[1]=="Freq." {
  qui drop in 1
 }
 qui tempfile mydata`var'
 qui save `mydata`var'', replace
}
qui {
clear
set obs 1
gen str3 t1=""
sort t1
foreach var of local varlist {
 merge t1 using `mydata`var''
 drop _merge
 sort t1
}
tempvar id
gen `id'=_n
forval num=1/`=_N' {
 set obs `=`=_N'+1'
 replace `id'=`num'+.5 in `=_N'
 sort `id'
}
foreach var of local varlist {
 replace `var'="(" + `var'594852963[_n-1] + ")" if `var'=="" &  `var'594852963[_n-1]~=""
}
drop *594852963
ren t1 values
drop in 1
drop `id'
} /* qui */
if "`auto'"=="" & "`dec'"=="" {
 local auto 2
}
local test="`excel'`word'`tex'"
if "`test'"=="" {
 local beg_dot = index(`"`using'"',".")
 if `beg_dot'~=0 {
  local strippedname = substr(`"`using'"',1,`=`beg_dot'-1')
 }
 else {
  local strippedname `"`using'"'
 }
 outsheet using `strippedname'.txt, nonames `replace' noquote
}
else {
 dataout, save(`using') `excel' `word' `tex' `replace' `noauto' auto(`auto') dec(`dec')
}
end
exit

 
_________________________________________________________________
Hotmail® has a new way to see what's up with your friends.
http://windowslive.com/Tutorial/Hotmail/WhatsNew?ocid=TXT_TAGLM_WL_HM_Tutorial_WhatsNew1_052009
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index