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

cd, mkdir

Read/write data

Stata

use, save, export

Automate folder creation

Both

mkdir before graph export

πŸ—οΈ 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#

  1. Could a non-Stata user run your code without opening it?

  2. Can you describe your folder layout to a collaborator in one sentence?

  3. 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 graph

  • code/ – write your master.do that automates loading, analyzing, exporting