Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

RE: st: RE: How can I append tabout output in Excel using logout?


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: RE: How can I append tabout output in Excel using logout?
Date   Wed, 28 Apr 2010 14:38:07 +0100

This is rather odd code. The outer loop is 

foreach y in trunk weight { 


}

But `y' is never referred to inside the loop. That's legal, but the effect is presumably just to do the same thing twice. 

Furthermore, the guts of the matter are equivalent to

tabstat mpg, stat(n mean median p25 p75 min max) by(foreign)

which, as you can see, is precisely one line. In other words, you don't need to repeat -tabstat- calls for different values of -foreign-,  because -tabstat- will do that unaided. 

As the results all go in a simple single table, there does not seem much, if any, need for an extra hierarchy of setting up log files and of calling programs to manipulate log files -- not that logging this and/or copying it elsewhere is ruled out in any sense. 

I know that you don't want to do precisely this, but something similar with your data, but some drastic economy should also be possible with your real problem if this is the coding style. 

This is more than a matter of style, as there seems to be plenty of evidence that people could save many hours of writing complicated code by a few hours of reading the documentation carefully and finding out about simpler ways of doing things. 

Nick 
n.j.cox@durham.ac.uk 

sara khan

Thank you all for responses.

I find formatting tables easier to do in Excel.

Roy Wada, the author of -logout- offered the following solution, which
worked for me:

* drop frivolous rows
sysuse auto, clear
local append replace
foreach y in trunk weight {
  forv s=0/1 {
   qui su mpg if foreign==`s'
   if r(N)!=0{
    caplog using logfile, `append': tabstat mpg if foreign==`s' ,
stat(n mean median p25 p75 min max )
    local append append
   }
  }
}
logout, clear use(logfile)
drop if t1=="variable" & _n~=1
logout, save(test_together) excel replace


* keep them
sysuse auto, clear
local append replace
foreach y in trunk weight {
  forv s=0/1 {
   qui su mpg if foreign==`s'
   if r(N)!=0{
    caplog using logfile, `append': tabstat mpg if foreign==`s' ,
stat(n mean median p25 p75 min max )
    local append append
   }
  }
}
logout, use(logfile) save(test_together) excel replace


On Wed, Apr 28, 2010 at 11:39 AM, Neil Shephard <nshephard@gmail.com> wrote:

> I don't really use Excel (other than to save data I've been sent to
> CSV for importing into Stata) or use M$-Windows, but I suspect that
> because of the proprietary nature of the Excel file format that
> -logout- is actually writing CSV files and files with the .csv
> extension are (generally) associated with the Excel program on most M$
> machines.
>
> If you _really_ want to do this (-collapse- and -statsby- will likely
> get you there quicker though), then you can write lots of individual
> files (with a slight tweaking of your code) and then concatenate them
> into one large file.  You can even do this from within Stata by making
> a shell call.
>
>
>>>
>>> forv i=0/5 {
>>> foreach y in male singlet{
>>> forv s=0/1{
>>> di "myga==`i' & `y'==`s'"
>>> qui su bwtg if myga==`i' & intab1==1 & admit_ic==1 & bwtg<. & `y'
>>>        if r(N)!=0{
>>>  logout, save("C:\data\do_files\mytab_`i'_`y'_`s'.txt")  excel replace: tabstat
>>> bwtg if myga==`i' & intab1==1 & admit_ic==1 & bwtg<., stat(n mean
>>> median p25 p75 min max ) by(`y') col(stat) f(%9.0g) notot nosep
>>>
>>> }
>>> }
>>> }
>>> }
>
> * Note that I've modified the above code so that each call to -logout-
> is writing the results to an individual file based on levels of i, y
> and s
> * Under a UNIX-like system the command to use is 'cat' and whilst
> you'd never have a "c:\" path the call would be
> !cat c:/data/do_files/mytab_* > c:/data/do_files/mytab.txt
>
> * Under M$-Windows you need to use the DOS Copy command (see
> http://support.microsoft.com/kb/69575)
> !COPY c:/data/do_files/mytab_* c:/data/do_files/mytab.txt
>
> Should do the trick, but as Nick says what is it that you can't do in Stata?
>
>
> Neil
>
> --
> "... no scientific worker has a fixed level of significance at which
> from year to year, and in all circumstances, he rejects hypotheses; he
> rather gives his mind to each particular case in the light of his
> evidence and his ideas." - Sir Ronald A. Fisher (1956)

*
*   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   |   Site index