Statalist The Stata Listserver

[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: writing output to ASCII file

From   "Peter J. Burke" <>
To   <>
Subject   st: RE: writing output to ASCII file
Date   Tue, 3 Oct 2006 13:08:10 -0700

	If there is no output, then check to be sure that the last line of
code is followed by a carriage return. It looks like the file close
statement is not executing. The code works on my computer.

Peter J. Burke
Department of Sociology
University of California
Riverside, CA 92521-0419
Phone: 951/827-7004
Fax: 951/827-3330

-----Original Message-----
[] On Behalf Of Scott Cunningham
Sent: Tuesday, October 03, 2006 11:54 AM
Subject: st: writing output to ASCII file

I am creating a table of summary statistics for individuals of  
different ages.  The data is from the National Longitudinal Survey of  
Youth 1997 (NLSY97), and the age of the individual range from 12 to  
23 (for my dataset, that is; respondents are older in the most recent  
waves).  I am calculating the number of sex partners individuals  
reported having in the last year, by age, using nested loops and the - 
file- command to store the output.  Below is my short program, which  
is very close to the one that Austin Nichols posted here a week or so  
ago wherein he recommended going this route to create and store so  
many statistics.  When I run this, it calculates the stats I need  
without trouble, but it does not appear to be storing them.  It is  
creating a file entitled sum.txt, but the file is empty.  So I assume  
the problem lies in the -file write...- code, or possibly with the  
nested loops themselves.  Can anyone see where I might be going wrong?




cap file close o
file open o using sum.txt, write replace
forv a=12/23 {
file write o _tab "`a'"
foreach v of varlist rp {
forv a=12/23 {
su `v' if age==`a', detail
local m`a'=r(mean)
local sd`a'=r(sd)
local n`a'=r(N)
local q50`a'=r(p50)
local q75`a'=r(p75)
local q90`a'=r(p90)
local q95`a'=r(p95)
file write o _n "`: var lab `v''" _tab
foreach stat in m sd n q50 q75 q90 q95 {
forv a=12/23 {
   file write o _tab "``stat'`a''"
file write o _n _tab
file close _all
*   For searches and help try:

*   For searches and help try:

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