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

st: Turning log files into do files


From   "Nick Winter" <nwinter@policystudies.com>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: Turning log files into do files
Date   Mon, 7 Oct 2002 10:38:13 -0400

I've whipped up the code below to turn a log file into a do file.  The
syntax is:

	. logtodo <logfilename> , saving(<dofilename>) [ replace ]

It does not deal with a smcl-formatted log; use -translate- first.  One
annoying feature
is that if the syntax that created the log file includes lines that are
broken over multiple lines using /* and */, these are strung together
into one long syntax line in the new do file.  But the resulting syntax
runs; it just doesn't look all that pretty.

I've only tested it a bit, but it seems to work.

program define logtodo
	version 7

	syntax anything(id="log file") , saving(string) [ replace ]

	confirm file `"`anything'"'
	if "`replace'"!="" {
		capture erase `"`saving'"'
	}
	confirm new file `"`saving'"'

	tempname hin hout

	file open `hin' using `"`anything'"' , text read
	file open `hout' using `"`saving'"' , text write

	file read `hin' line

	while !r(eof) {
		if substr(`"`line'"',1,2)==". " {
			local line : subinstr local line ". " ""
			file write `hout' `"`line'"'
			file read `hin' line
			while substr(`"`line'"',1,2)=="> " {
				local line : subinstr local line "> " ""
				file write `hout' `"`line'"'
				file read `hin' line
			}
			file write `hout' _n
		}
		else {
			file read `hin' line
		}
	}

	file close `hout'
	file close `hin'

end

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