Medical statistics and Data Science: Statistics

Intermediate level 1 in Stata

Preface

Create a log file

Sometimes, we would like to create a file recording results appear in the Results window. In Stata, log file fulfils this purpose. Log file can record everything that appears in the Results window, including not only results, but also the Stata commands that producing the results as well as any errors during the process.

As discussed in the How to get started with Stata, there are three ways communicating with Stata. Accordingly, there are three ways creating a log file, namely by clicking on menu, type commands in Command window, and type commands in do-file editor. Here, we recommend and show how to do it via do file.

In order to create a log file, you have to give a name to the log file. You may name a log file whatever you prefer but it is a good idea to give the same name as the do file. As the following example, if your do file is named as analysis, that is marked as the dark blue, then you name your log file as analysis.log.

  • Step 1: At the top of the do file, you write "log using analysis.log, replace". This command line initiates a log file that is named as analysis.log. Here "analysis" is just an example of naming a log file. You certainly can give a different name.
  • Step 2: at the bottom of the do file, you write "log close". This command line closes the log file
  • Step 3: Inbetween the two lines, you type whatever you would like ask Stata to do

Before you run any commands, it is important to run the line "log using analysis.log, replace". Afterwords everything that you do will be recorded and there should be log file (analysis.log) saved in your computer. If you open the log file, it records everything that you have done. In the end, to close the log file by running the line "log close"

String variable versus numeric variable

Change working directory

Merge two dataset

Under development

Append two dataset

An overview of Stata command

The description of the command Name of command Example
log
destring
cd
merge
append