5 The do file
Do files are Stata’s scripts: simple programs made of text files of Stata commands.
Let’s start off by creating a new do file, type
doedit newdo.do
on the Stata console. You can also create a new do file editing session from the Window menu in Stata.
Clear everything to make sure there’s nothing leftover in our environment
clear all
5.1 Initialising your script
In a new data wrangling script you should
- start a log;
- make sure you are in the correct project directory;
- clear working memory.
So to start this process, in your new do file add these lines (but substitute the name of your own Stata project for "ProjectName"):
capture log close
log using "MainProjectName $S_DATE.log", append
and
capture cmdlog close
cmdlog using "CommandsProjectName $S_DATE.log", append
The first will log all commands and results window ouput (ie not graphs) the second will log commands only and not output. As you gain more experience you will probably choose to use only one of these logs.
The commands use a Stata system variable $S_DATE
to insert today’s date into the file name of your logs. This makes tracking your work easier.
5.2 Exercise
Using your internet searching powers, find out why we wrote
capture log close
at the start of the script, rather than just log close
.
5.3 Which directory?
Next you should make sure you are in the correct directory.
To check which directory you are currently in type
pwd
on the Stata console.
5.4 Change to your data directory
Change directory to the folder that will hold your project’s raw data files (that is data that you have not processed or transformed). Normally, for a data wrangling script, this will be the raw_data
directory for the project and you change to that directory by typing a line like
cd c:\users\jt\Documents\Projects\StataWrangling\raw_data\
(you must alter this to point at your folder). When you write code to read a raw data file, you will want to make sure that it is read into this directory.