Conversion of Stata log files to do files
log2do2 logfile , saving(filename [ {replace|append} ]
Description
log2do2 extracts the command lines from a Stata text-format log file, creating a file of syntax (ie, a do file).
The program recognizes commands as lines in the log that begin with a period followed by a blank space. It also recognizes command that are wrapped over multiple lines by Stata, and command that appear within foreach and forvalues loops.
log2do2 uses the Stata 7 file command to read and write the file. This command was in an update to Stata; be sure your stata is fully up to date. The use of file allows log2do2 to preserve references to local macros in command lines. The if the following line appears in a log file:
. display "local x is `x'"
then the reference to `x' will appear in the do file as `x', rather than being expanded by Stata and, therefore, blanked out.
Note that the program requires a text-format log file, and will not work with a smcl-formatted log. See translate to translate a smcl log into plain text format.
Things to watch for
Several situations can confuse log2do2. First, the block of code within a forvalues or foreach loop are generally displayed in the log file as numbered lines, rather than being preceded by a period and a space. log2do2 handles this by noticing the beginning of the loop, and counting the open- and close-braces (ie, -) and )- to find the end of the block of code. This works correctly unless there are unmatched brace characters in comments within the looped code.
Second, Stata code can be written that will make output look to log2do2 like input lines. For example, the command display ". hello there" will lead to the line "hello there" appearing in the do file.
Finally, if the log file was created by do file that split commands over multiple lines, through use of the /* and */ delimiters, then log2do2 will string the command together on one long line. The resulting do file will work correctly, but might need editing for style points.
Example
. log2do2 "c:/My Files/mylog.log" , saving(c:/My Files/mydo.do) replace
Author
Nick Winter Cornell University nw53@cornell.edu