Opening and saving Stata datasets

Opening and saving datasets in Stata works similarly to those tasks in other computer applications. There are a few differences, however. First, it is possible to save and open files from within Stata’s Command window. Second, Stata allows just one dataset to be active at any one time. That is, while it is possible to have multiple datasets in memory at once, only one dataset may be active. Keeping this in mind will make Stata’s care in opening new datasets clear. This post outlines all the possible ways to open and save datasets (download datasets using in the following video).

A Stata dataset can be opened in a variety of ways, most of which are probably familiar to you from other applications:

  • Double-click on a Stata data file, which is a file whose extension is .dta. Note: The file extension may not be visible, depending on what options you have set in your operating system.
  • Select File > Open… or click on the Open button and navigate to the file.
  • Select File > Open data subset…, navigate to the file, specify the observation range, and select variables from the dataset.
  • Select File > Recent files > filename. 
  • Type use filename in the Command window. Stata will look for filename in the current working directory. If the file is located elsewhere, you will need to give its path. Be aware that if there is a space anywhere in the path or filename, you will need to put the filename inside quotation marks.
  • Type sysuse filename in the Command window. Stata will look for filename in a series of directories called the adopath. Typically, this is for finding example datasets installed when you installed Stata, but it can also be used for easy access to your own datasets. For more information on the adopath.
  • Type webuse filename in the Command window. The webuse command is used to access datasets used in the Stata manuals; for example, webuse lbw loads the lbw dataset used in the documentation of the logistic For more information, see [D] webuse.

Opening a dataset in the current frame will replace the dataset, if any, that is currently in memory for that frame. Datasets in other frames are unaffected. If there have been changes to the data in the dataset in the current frame, Stata will refuse to discard the dataset unless you force it to do so. If you open the file with any method other than the Command window, you will be prompted. If you use the Command window and the current data have changed, you will get the following error message:

. sysuse auto

no; dataset in memory has changed since last saved

r(4);

These behaviors protect you from mistakenly losing data.

To save an unnamed dataset (or an old dataset under a new name):

  1. select File > Save as…; or
  2. type save filename in the Command window.

To save a dataset for use with Stata 13,

  1. select File > Save as…, and select Stata 13 Data (*.dta) from the Save as type list; or
  2. type saveold filename in the Command window.

To save a dataset that has been changed (overwriting the original data file),

  1. select File > Save;
  2. click on the Save button; or
  3. type save, replace in the Command window.

Once you overwrite a dataset, there is no way to recover your original dataset. With important datasets, you may want to either keep a backup copy of your original filename.dta or save your changes to a dataset under a new name. This is no different from working with a word-processing document, except that recovering from an inadvertent save of a dataset is nearly impossible.

Important note: Changes you have made to a dataset are not permanent until you save them. You work with a copy of the dataset in memory, not with the data file itself. This should not be surprising, because it is the way that you work with almost all applications on your computer.

If you do not want to save your dataset, you can clear the dataset in memory and open a new dataset by typing use filename, clear.

Source: STATA (2021), Getting Started with Stata for Windows, Stata Press Publication.

1 thoughts on “Opening and saving Stata datasets

  1. Katy Wenstrom says:

    Sweet blog! I found it while surfing around on Yahoo News. Do you have any tips on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Appreciate it

Leave a Reply

Your email address will not be published. Required fields are marked *