*! version May 1, 2014 @ 09:23:49

Title

smcl2do -- convert a SMCL log file to a do-file

Syntax

smcl2do
using filename
[,
options
]

saving(do-file) file in which to save the do-file

replace replace do-file if it exists

all keep all commands, even those which caused errors

clean get rid of non-programmatic commands, such as help and edit

Description

smcl2do turns a SMCL log file into a do-file. By default, smcl2do strips all commands which resulted in errors.
Thus, the resulting do-file should run flawlessly (except for such things as writing files which already exist).

Options

saving(do-file name) gives a filename to save the resulting do-file.
If not specified, the name defaults to the using filename with a .do extension.

replace allows an exising do-file name to be overwritten.

all writes all commands from the log file to the do-file, including those which did not complete.

clean strips out commands which have no direct consquence for the data, dataset, or any analysis:
browse, db, doedit, edit, help, projmanager, search, varmanage, view, and viewsource.

Remarks

smcl2do turns a SMCL log file into a do-file, doing its best to ensure that the resulting do-file will run.
This can be very useful for recovering commands from a long logged Stata session, because it can be done long after the session is over.

smcl2do can handle many complicated constructs: loops, Mata, wrapped lines, do commands, and continued commands.

The default extension of the using filename is .smcl and the default extension of the saved file is .do.

If you would like to experiment with it, try making a do-file which keeps a log, and then use smcl2do to turn the log file back into a do-file.
You should see that the resulting do-file will match the original well, except for keeping its own log.

There are a few restrictions which are needed to make the resulting do-file work properly:

If you have do commands within loops, they must be run quietly. Without quietly the commands echoed by the do-file get included in the do-file created by smcl2do, and hence would be run twice.

Mata blocks should be started with

mata

and not with

mata:

The problem with the latter form is that any Mata command which causes an error jumps back to Stata without an end command, thus making any resulting do-file buggy.

The maximum length command is 255 characters, because this is the maximum linesize which can be handled using the translate command.

Do not use any {com}. or {com}: SMCL directives (you shouldn't be doing this anyway).

Examples

. smcl2do somelog

turns the log file somelog.smcl into the do-file somelog.do, excluding all the commands which did not complete because of errors or interruptions.

. smcl2do somelog, all

turns the log file somelog.smcl into the do-file somelog.do, including all the commands from the log file, including those which did not complete.

. smcl2do somelog, clean

turns the log file somelog.smcl into the do-file somelog.do, excluding all the commands which did not complete and also excluding commands such as help and edit.

Stored results

None

Author

Bill Rising, StataCorp

email: brising@stata.com

web: http://louabill.org/Stata