1. π§ Collaborative Stata Programming#
Here are the class notes weβd initially prepared for you, until the spirit of the day led us astray!
We embraced the spirit and our digression now replaces the main branch (a GitHub reference).
π― Objectives#
Understand the importance of directory structure for collaboration and reproducibility
Learn key system-level and Stata-specific commands for working with files
Write OS-agnostic, path-sensitive Stata code
Think like a collaborator: your code should run for someone else without edits
π Why Directories Matter#
Modern data work β especially in collaborative settings β depends on clean, predictable organization. Your folder structure is not just a container, itβs a map others will rely on.
A good structure:
Saves time (and money, if using compute credits)
Reduces errors in file paths
Builds trust with collaborators and co-authors
π§ System-Level vs. Stata-Level#
Task |
Type |
Example |
---|---|---|
Change folder |
System-level |
|
Read/write data |
Stata |
|
Automate folder creation |
Both |
|
ποΈ Example Project Structure#
project/
βββ data/ # .dta files
βββ output/ # Graphs, tables, logs
βββ code/ # .do files
βββ notes/ # README, protocols
βββ master.do # Runs everything
// Set root and create folder
cd "~/Documents/project/"
mkdir output
// Load a dataset
use "data/sample.dta", clear
// Save a graph
graph twoway scatter y x
graph export "output/scatterplot.png", replace
π₯οΈ Writing OS-Agnostic Code#
Different collaborators may use Windows, Mac, or Linux. Paths differ (\
vs /
). You can detect the OS using Stataβs system variable c(os)
:
di c(os)
if c(os) == "Windows" {
use data\transplants, clear
list age in 1/10
}
else {
use data/transplants, clear
list age in 1/10
}
π€ Reflection Questions#
Could a non-Stata user run your code without opening it?
Can you describe your folder layout to a collaborator in one sentence?
Are your outputs stored in predictable, clean locations?
π§πΎβπ» Assignment#
Create your own project folder with this layout:
data/
β include a sample.dta
output/
β save a log and graphcode/
β write yourmaster.do
that automates loading, analyzing, exporting