Managing Working Directories (Folders)

A directory (also known as a folder) is a place on your computer or network to store and sort files. An example of a directory is the Documents folder on your Windows or Mac computer.

Stata needs to work from a directory on your computer. It automatically loads a directory when it starts. The default directory Stata loads when opening is:

  • For Windows – C:/Users/Username/Documents

  • For Mac – ~/Users/Username/Documents

  • For Linux – ~/Home/Username/Documents

You can identify your current working directory using the pwd command (short for "Print Working Directory"). If you have only just loaded Stata for the first time this command should show you one of the paths listed above.

For practical reasons you might want to operate from a different directory when using Stata. While it is helpful that Stata stores the files in this single location, it will quickly become cluttered if you put all your datasets and do-files in this one directory.

The mkdir command allows you to create a new directory from within Stata. Using this command without specifying the full path will create a new directory within your current working directory. You can also specify the full path (making sure to enclose the path in double-quotes "") to create a specific folder anywhere on your computer or network. You then use the cd command to change the current working directory to your newly created directory. If you already have a directory in mind, you can simply use cd to change directories.

How to Use:

To create a directory within your current directory:

To create a directory somewhere else (replace example with your own path):

To change your current working directory to your new folder:

If the directory name has a space in the name, the directory name must be enclosed in quotation marks.

Worked Example 1:

I am in my Documents folder, the default directory for Stata, and I want to create a folder for my Stata data and then make that my current working directory. In the command pane I type the following:

This gives the following output in the Stata results pane:

If you followed along with this example and you have yet to change your current working directory, your working directory should now show as being in the "stata" sub-folder of your Documents folder.

Worked Example 2:

I have created the stata directory in my Documents directory, and then I closed out of Stata. When I returned and reopened Stata I found that my working directory was my Documents folder again even though I just changed it to the stata directory. The reason the working directory for this new session of Stata is my Documents folder again is because Stata regards my Documents folder as the default. Stata will usually load in here unless I tell it not to.

Now that I have reopened Stata I am starting a new project, let's call it "Statistics 101". I would like all the files for this project to be contained in my new stata folder, however I would like to keep them separate from other projects I might be doing in the future. To keep this project separate I am going to create another sub-folder within my stata folder. Since Stata has returned me to my Documents folder, I will need to specify the path name to create my new folder this time. I am going to call this folder "Statistics 101", and I will make this my current working directory for this session.

In the command pane I type the following:

This gives the following output:

Here you can see I have typed the full path name of the directory, and I have used quotations. The quotations are necessary because the “Statistics 101” directory name has a space in it, and so for Stata to recognise it as a full directory name it needs quotations. The full path name is necessary because my current working directory is Documents, not Documents\stata. Stata would have returned an error if I had simply typed cd “Statistics 101”. This is because the command cd "Statistics 101" when used in my current working directory (Documents) is actually asking Stata to change to C:/users/My Name/Documents/Statistics 101, which does not exist.

Note: In the above examples I have used a Windows version of Stata. The path for a Mac or Linux version would differ only by the location of the Documents folder. To find out your current working directory you can always type pwd in the command pane.

654 views0 comments

Recent Posts

See All