Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: writing output with -file- command


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: writing output with -file- command
Date   Tue, 17 Dec 2002 17:54:56 -0000

Richard Aplenc
>
> I would like to write the output of a nested -foreach- loop
> to a .txt
> file with the -file- command, and am having some difficulty
> doing so
> successfully.
>
> My dataset consists of patients with length of chemotherapy
> courses and
> variables for gender, race, and treatment, all of which are binary
> variables.  I would like to be able to compare length of
> chemotherapy
> course by sex, race, and treatment and output the mean, sd,
> and p value
> for each comparison.
>
> I would appreciate help in using the -file- command to output these
> values to a .txt file.
>
> My code is as follows:
>
> local variables "sex race treatment"
> local courses "course100 course200 course300 course701 course702
> course703..."
>
> foreach course of local courses {
> 	foreach variable of local variables {
> 		ttest `course', by(`variable')
> 	}
> }
>
> Thanks in advance for the help and my apologies for not
> being able to
> figure this out on my own.

Which mean? Which sd?

The following will get you started:

program def aplenc
	version 7
	syntax varlist using/, by(varlist)
	tempname out

	file open `out' using `"`using'"', w

	foreach v of local varlist {
		foreach b of local by {
			qui ttest `v', by(`b')
		      file write `out' /*
	*/ %12s (abbrev("`v'",10)) %12s (abbrev("`b'",10)) /*
	*/ %9.4f (r(p)) _n
		}
	}
	file close `out'
end

Your main task is to fiddle with the -file write-
line.


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

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   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