Stata: Do-files and Log-files

As you begin to work with data sets, there are two records and saving your commands and actions in Stata.

Creating do files

Do files allow you to record all of your commandments. There are a number of benefits to using do files. By using do-files to run your commands, you have a copy of what you did, allowing you and other researchers to replicate your analyzes exactly. It also allows you to run analyzes without changing your original data file until you are ready to save a clean data set. Many researchers will maintain a do file log data management (addressing missing data, reverse scoring if necessary, etc.) and may have separate do files for analysis for the final clean data set or subsets. To create a do file, you can go to “File”->”New do file” or you can use this icon on the toolbar in the Stata window.

Your new do file should open in a separate window that looks like this:

An optional step that can be useful in creating do files is to place a comment at the top of the file indicating what data you are using and any other notes you want. To separate notes and comments for commands in do files, start the line with an asterisk. If it’s a longer note, you can differentiate it by writing /* before your comment and */ after the comment. For example, if I were using a data set called “relate”, I could start my data file like this:

See Also:  8 tips for creating ideal website backgrounds

The first command you’ll need is the use command to specify the file you want Stata to use . If the file is not in the working directory you are currently in, simply specify which directory you want to extract the file from. Here are three examples of the use command, one from a dataset in the current job directly, one from the Internet, and one from a jump drive in a different working directory. Note that at the end of each command, I add the delete option. This is to delete all data that Stata is currently working with.

use relate.dta, delete use http://www.stata-press.com/data/agis3/relate, delete use “E:
elate.dta”, delete

After specifying the data file, I enter the rest of the commands I want to execute. Inside this file, Stata will assume each line is a new command unless I tell it otherwise. a long command that you need on separate lines, add /// to the end of each line. That tells Stata that the next line is part of the same command. When I’m ready to run the analyses, I select the commands I’d like to run ( you don’t have to select any text if you want to run them all) and click the last icon on the toolbar in the do-file window:

To save your do-file, you can use the icon in the do-file toolbar or use the “File”->”Save As” menu while the file editor is active.

See Also:  How to connect 8 speakers to a 4 channel amp

Creating log files

In addition to logging all your commands in a do file, you can also have Stata create a copy of everything that is sent to the sale. ana Results, with the exception of graphs. This is called a log file and may be useful to you. to save all your output. This will also retain your commands, although it won’t save them the way a do file does (they’ll be embedded in the output). To create a log file, go to “File” -> “Log” -> “Get Started”. This will open a dialog where you will save your log file. The default in Stata is to save the file with the .smcl extension. This will allow you to open the log file in Stata, but other programs will not read this type of file. The other available extension is .log. This file format will allow you to open your log file in other programs and may be easier to manage than .smcl files. To save it as a .log file, simply select the Stata Log option from the “File Format” menu in the dialog box.

Once you start a log file, you can stop it at any time and resume it. later. You can do this by going to “File” -> “Log” -> “Suspend” (or “Resume”). You can also close your log using this menu.

You can also start, suspend, resume, and close logs using the log command. I’ll use this command to start a log file, specify the file name and location, as well as the extension. If I were to create a log file called “log file creation” in a file on my desktop called “501” (file path: /Desktop/501), I would write:

See Also:  How to Make Your WordPress Blog Completely Private (4 Ways)

log using “/Desktop/501 /creatinglogfiles” , text

I included text because I want the file to be a .log file, not a .smcl file. If I wanted to overwrite a file that already exists, I would add replace after the text.

After opening the log file, typing close will suspend the log file, logging will resume the log file and log close will close your log file.

.

Leave a Reply

Your email address will not be published. Required fields are marked *