Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

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

re: st: data management question

From   "Airey, David C" <[email protected]>
To   "[email protected]" <[email protected]>
Subject   re: st: data management question
Date   Fri, 15 Oct 2010 15:22:20 -0500


Thanks Nick and Eric.

I ended up with something equivalent to what is below, before seeing Eric's post:

sysuse auto

collapse (mean) mpg weight ///
	(sem) sem_mpg=mpg sem_weight=weight ///
	(count) n_mpg=mpg n_weight=weight, by(foreign)

order foreign mpg sem_mpg n_mpg weight sem_weight n_weight

outsheet using data.txt, nonames replace

/* open files to read and write */

tempname myfile // makes closing files automatic
tempname mydata

file open `myfile' using test.txt, write text
file open `mydata' using data.txt, read text

/* write header info */

file write `myfile' "# comment1" _newline
file write `myfile' "# comment2" _newline
file write `myfile' "@format=column" _tab
file write `myfile' "mpg" _tab
file write `myfile' "SE" _tab
file write `myfile' "N" _tab
file write `myfile' "weight" _tab
file write `myfile' "SE" _tab
file write `myfile' "N" _newline

/* read outsheeted data from data file 
and write to text file */

file read `mydata' line

while r(eof) == 0 {
	file write `myfile' `"`line'"' _newline
	file read `mydata' line

/* close files */

file close `mydata'
file close `myfile'

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index