Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 36 ยท Interface and Data

Working Directory and File Paths

Control where Stata reads and writes files: cd, pwd, absolute and relative paths, quoting names with spaces, a project folder layout and portable paths with global macros.

Every file command in Stata, such as use, save, import and log using, needs to know where the file is. Most beginner errors in the first week are file not found (return code r(601)) because Stata looked in a different folder than the one you imagined. This lesson explains the working directory, absolute and relative paths, and a simple project layout that keeps your files organized and your do-files portable.

The working directory

Stata always has a current working directory. When you type a filename without a folder, Stata looks there.

STATA
pwd                          // print the current working directory
cd "C:/projects/wages"       // change it (Windows)
cd ~/projects/wages          // macOS or Linux
dir                          // list the files in it (also: ls)

pwd stands for "print working directory". After cd, commands like use survey.dta find the file in C:/projects/wages.

Two Windows details:

  • Use forward slashes (C:/projects/wages) in do-files. Backslashes work in simple cases, but Stata also uses the backslash as an escape character in strings and macros, so they sometimes break. Forward slashes work on every operating system.
  • Quote paths that contain spaces: cd "C:/My Documents/wages". Quotes are harmless on paths without spaces, so many people always use them.

Absolute and relative paths

An absolute path starts from the root of the drive:

STATA
use "C:/projects/wages/data/raw/survey.dta", clear

A relative path starts from the working directory:

STATA
use data/raw/survey.dta, clear      // the folder data/raw inside the working directory
use ../shared/codes.dta, clear      // one folder up, then shared/

.. means the parent folder. Relative paths make a project portable: if you zip the whole project folder and send it to a colleague, relative paths still work, but absolute paths such as C:/Users/tehra/... break on their computer.

A project folder layout

Use one folder per project with a consistent structure:

Text
wages/
  do/            do-files (01_import.do, 02_clean.do, 03_models.do)
  data/
    raw/         original data, never edited
    clean/       datasets produced by your do-files
  output/
    tables/
    figures/
  logs/          log files

The key rule: never change files in raw/. Your do-files read from raw/ and write to clean/, so the whole chain can be rerun from scratch at any time.

flowchart LR
    A["data/raw<br/>(read only)"] --> B["do/01_import.do"]
    B --> C["data/clean"]
    C --> D["do/02_models.do"]
    D --> E["output/tables<br/>output/figures"]
    D --> F["logs"]

Raw data flows through do-files into clean data, tables and figures; nothing flows back into raw.

One root, many paths: global macros

Typing the project root over and over is error-prone. Set it once at the top of a master do-file and build every other path from it using a global macro:

STATA
* master.do
clear all
global root "C:/projects/wages"      // the only line a new user edits
cd "$root"

use "$root/data/raw/survey.dta", clear
save "$root/data/clean/survey_clean.dta", replace
log using "$root/logs/analysis.log", replace text

global name "text" stores text; $name (or ${name}) pastes it. Use the braces when the next character could be read as part of the name: ${root}_backup. A macro is just text substitution, so "$root/data" becomes "C:/projects/wages/data" before Stata reads the line. We cover macros in depth in the programming chapter.

Another handy tool: the c(pwd) system value gives the working directory, so a do-file can build paths without any user edit if it is always run from the project root:

STATA
local root = c(pwd)

Creating folders and checking files

STATA
mkdir output                         // create a folder (error if it exists)
capture mkdir output                 // ignore the error if it already exists
confirm file "data/raw/survey.dta"   // stop with an error if the file is missing
capture confirm file "data/raw/survey.dta"
display _rc                          // 0 = found, 601 = not found

capture runs a command and swallows any error; the result is stored in the return code _rc. This is the standard way to test for a file or folder.

Other useful file commands

STATA
copy "a.csv" "b.csv", replace       // copy a file
erase "temp.dta"                    // delete one file
shell dir                           // run an operating-system command (Windows)
filelist, dir("data") pat("*.dta")  // user command: list files (ssc install filelist)

erase deletes for good, with no recycle bin, so double-check the path.

A path joiner stand-in

Building paths by pasting strings is where slashes go wrong. This stand-in models joining and normalizing a root and a relative path, like "$root/data/raw" and .. handling. It is a Python model of the idea, not Stata's own code.

Python Stand-in: absolute vs relative paths

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Stand-in: why moving a project breaks absolute paths

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which command shows the current working directory?

    pwd prints the working directory; cd path changes it.
  2. 2. Why are forward slashes recommended in Stata do-files?

    Forward slashes work on all operating systems and avoid escape problems.
  3. 3. What is a benefit of relative paths?

    Relative paths depend only on the project structure, not on one person's drive layout.
  4. 4. What does capture confirm file "x.dta" followed by display _rc print when the file is missing?

    confirm file fails with return code 601 when the file cannot be found.

Summary

  • pwd, cd and dir show, change and list the working directory.
  • Use forward slashes and quotes; avoid absolute paths in shared code.
  • A project layout with raw/ (untouched), clean/, output/ and logs/ keeps work reproducible.
  • A global macro $root set once lets every path stay portable.
  • capture plus confirm file and _rc test for files; r(601) means not found.

Next: Importing Data