Stata Help

Directory Navigation in Stata

For all intents and purposes, "working directory" is tech-speak for "whatever folder Stata thinks you're in." This is relevant because when you run commands that involve files, the working directory is where Stata will look for those files.

Which directory Stata is working in is always displayed at the bottom of the Stata toolbar

If the wrong directory is being displayed, there are two ways to change it. The first way, which is perhaps also the easiest for those who are new to command line (non-graphic) interfaces is to use the menus, File => Change Working Directory. From here you can navigate to wherever the file(s) you are going to use is/are stored. Select a folder by double-clicking on it, then click Choose.

When you do so in this way, you'll notice the Review window displays the command cd followed by a series of folder names separated by slashes. This command, the same as used in Terminal or a Windows command prompt, is short for "change directory" and is the second way to switch the current working directory.

The two commands needed to navigate like this are cd and ls. The latter (ls) causes Stata to list the contents of your current directory. cd moves you into the specified directory, provided you include the proper path to the file.

A useful alternative way to think about file paths, since the language is a bit nonintuitive, is as an address if everyone lived in a tree. With this analogy, you can consider a program like Stata to be a tree postman. The very bottom of your tree is the hard drive itself, known as the root directory, its address is "~"

However since you are logged in to the computer as a user, the next part of the address is predetermined: /Users/yourusername. In addition, Stata assumes you are working in the Documents folder, which leaves you on the tree branch that is your document folder: Users/yourusername/Documents (this should in fact be what displays in the toolbar when you open Stata).

Now let's say you actually have the files you want for this Stata session in a folder of Psychology data. Specifically, the data is in Documents => Thesis => Thesis Data => Experiment 1.

Assuming I know for certain that is the full set of folders I have to go through (and that I'm starting from Documents) I could type cd "Thesis/Thesis Date/Experiment 1" Since my file path has spaces, I use quote marks around the whole thing (otherwise, Stata will not understand where you want to go)

If I was less certain what the precise file path was, I could cd one level at a time, using ls to see where to go next.

One last useful command for navigating is cd .. The .. tells Stata to back up one Directory.

While at first this may sound difficult to manage, with a little practice you'll be navigating your directories with file paths in no time. 

Back