Statalist The Stata Listserver


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

st: writing output to ASCII file


From   Scott Cunningham <scunning@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   st: writing output to ASCII file
Date   Tue, 3 Oct 2006 14:53:41 -0400

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?

thanks,


scott

**

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:
* 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