-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpart5.Rmd
203 lines (130 loc) · 5.11 KB
/
part5.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
---
title: "Stata Training"
author: "Linda Wang"
date: 'April 2023'
output: slidy_presentation
---
# How to Run Stata Code
##
These slides are based on the [Stata-related procedures](https://labordynamicsinstitute.github.io/ldilab-manual/96-00-using-Stata.html) section of the training manual.
## config.do
![](images/stata_config_new.png)
![](images/stata_config_new_2.png)
![](images/stata_config_new_3.png)
## Purpose
- Generate log files.
- Install programs.
## Set up
- Rename template-config.do and place it in the same directory as the .do file(s) you will run.
- Add lines to the file(s) so that they execute config.do when run. To clarify:
- If there is no master.do file, add `include config.do` at the beginning and `log close _all` at the end of the .do file(s) you will run.
- If there is a master.do file, add the lines to it, only.
### Walkthrough
#### Package Installation
- Indicates the structure of the code.
- Installs Stata packages needed for replication.
- You will need to add package names to line 50. (Use line 52 as an example).
#### Present/Working Directory Set Up
- Sets file path to root directory (based on the structure indicated previously).
- Creates a sub-directory to save logs in, if necessary.
- Keep this area even if authors' code also produces log files.
#### Diagnostics
- Provides information about the system the code is run on, for the replication package.
#### Other
- Includes lines for specific cases, e.g.
- Net installation of packages
- mata
- S drive path set up
## master.do
If the authors do not provide a master .do, but the order in which each program is run is clear (either from file names or from the README), then create a master.do as follows:
![](images/doeditor.png)
##
![](images/doeditor2.png)
##
- Add `include config.do` for the first line and `log close _all` for the last line.
- For each program that needs to be run, write `do` and the path. Make sure these lines are in the right sequence.
```
include "config.do"
* Assuming scenario "A"
do "${rootdir}/code/0_first_program.do"
do "${rootdir}/code/1_second_program.do"
do "${rootdir}/code/2_third_program.do"
do "${rootdir}/code/appendix_code/appendix.do"
log close _all
```
##
![](images/master-example.png)
##
![](images/code_repo.png)
## General Stata Tips
## Modifying File Paths
- Typically will require one modification to either the master .do file or to a program called by the master .do file.
Before:
```
*/ This is Master do file /*
global maindir "C:\Users\Author\Dropbox\Project1" // this is the path to the repository
global data "$maindir/data" // path to data folder
global figures "$maindir/figures" // path to figures folder
```
After:
```
include "config.do"
*/ This is Master do file /*
global maindir "$rootdir" // this is the path to the repository
global data "$maindir/data" // path to data folder
global figures "$maindir/figures" // path to figures folder
```
##
- Certain errors may indicate that a subdirectory is missing. If the programs/README/other documentation verify this, then you should create the missing directories.
E.g.
```
log using "${path}\Output\maindata.log", replace
(file U:\AEAworkspace\aearep-3835\182763\Output\maindata.log not found)
file U:\AEAworkspace\aearep-3835\182763\Output\maindata.log could not be opened
r(603)
```
##
- Also note that the config .do file has a parameter called `scenario` that is set to `A` by default.
- `A` indicates the case where the master .do file is in a subfolder of the root directory.
- `B` indicates the case where the master .do file is in the root directory. Change the default if needed.
Scenario A
```
directory/
code/
main.do
01_dosomething.do
data/
data.dta
otherdata.dta
```
Scenario B
```
directory/
main.do
scripts/
01_dosomething.do
data/
data.dta
otherdata.dta
```
## Running the Code
- Right click on the master .do file and select the option `Execute (do)`. This will set the working directory location to the location where the master .do file is, automatically.
![](images/execute-do.png)
##
- To run Stata on Git Bash from CISER, use the following command (note that relative paths can also be used):
```
"C:/Program Files/Stata17/StataMP-64.exe" -b do "U:/test/master.do"
```
- To run Stata on Powershell on Windows (e.g. personal laptop), use the following command:
```
'C:\Program Files\Stata16\StataMP-64.exe' /b do .\master.do
```
- To run Stata on Linux/MacOS, use the following command:
```
stata-mp -b do master.do
```
- Note that relative file paths to the .do file can also be used.
## Missing Package(s)
- Typically indicated in error messages, e.g. ``command not found`` or ``type ssc install``, and can be resolved through modifications to config.do.
- Note that the potentially missing packages flagged by `scan_packages.do` may be generating some of these errors.
- As a reminder, don't add those flagged packages to `config.do` before running code for the first time. (An exception would be if the README indicates long runtimes.)