For example, you may have in your main.do file the following:
//constants
local PROJECT_CODE "myProject"
local DATA_DIR "P:/VDEC/statatemp/`PROJECT_CODE'"
local LIB_DIR "P:/VDEC/LIB"
local STUDY_START_YEAR = 2001
local STUDY_END_YEAR = 2015
...
//load dependencies
include "`LIB_DIR'/lib1.do"
include "`LIB_DIR'/lib2.do"
//call my subroutines
include "extractData.do"
include "analyzeData.do"
...
Then, in extractData.do, you may have something like:
local var1 "diab"
local var2 "asthma"
...
use `DATA_DIR'/data1
drop if year(indexDate) < `STUDY_START_YEAR'
...
In a nutshell, Stata concatenates all your code, including library routines and your project-specific subroutines, into one single gigantic file. As a result: - all your “local” macros/variables (e.g., `var1’ in extractData.do) are now visible to all the code you ‘included’ after defining that macro which could result in subtle and hard-to-find bugs, - the behaviour of the code in your project-specific subroutine files depends on the order of their inclusion in main.do because they rely either intentionally or unintentionally on code defined in other subroutine files, - there is no true encapsulation or isolation of concerns, and - there is limited potential for reusing the code.
stata
//include "`LIB_DIR'/lib1.do"
do "`LIB_DIR'/lib1.do"
\\ in extractData.do
capture program drop extractData
program define extractData
syntax, dataFileName(string) studyStartYear(int) ...etc
local var1 "diab"
local var2 "asthma"
...
use `dataFileName'
drop if year(indexDate) < `studyStartYear'
...
end
Now at the top of main.do
do extractData.do
When you need to call extractData
extractData, dataFileName("`DATA_DIR'/data1") studyStartYear(`STUDY_START_YEAR')
syntax
command is very powerful and allows for validating the existence and types of variables passed as arguments, automatic handling of if
statements, correct (os-independent) handling of filenames and much more. It also makes your programs easier to use because they follow the same rules as built-in Stata commands. For passing 1 or 2 parameters, you could also use the simpler command args
(although you will likely regret that once you need to edit the program).