Preserve and Restore Data in Stata

Preserve and restore commands in STATA offers invaluable help, especially working with large data sets. Suppose you are working on a data set and for some purposes, you have altered the data set but now you want to retrieve the original data set and run operations on it. you can do that by using simple operations in STATA. To understand the procedure, follow the below procedure.

Upload the data set into STATA. We are using the inbuilt dataset and the syntax for using such data is

sysuse auto.dta,clear

Now, we will alter the data by using the following commands:

collapse (mean) price mpg weight, by(foreign)
Related Article: Aggregate Data by Group using Collapse command in Stata

By doing so our original data set has been altered and the output window will only display the following outputs


Since our original data has been changed and we are not able to perform any operations on the original data set. That’s where the preserve command helps us. The preserve command helps us to retrieve the original data set even after altering the data set. While working with preserve command it must be kept in mind that it needs to be run simultaneously with other operations.  

sysuse auto.dta,clear  preserve  collapse (mean) price mpg weight, by(foreign)  list in 1/2

By running the above commands we will get the below output.


Observe that, in the above picture output window, the initial data set has been preserved but at the same time, we have achieved the mean value of price, mpg, and weight.

Similarly, the restore command can also help us to restore our original data set even if the data is being altered. To do so follow the commands:

sysuse auto.dta,clear preserve drop price mpg

By running this command, the variable price has been dropped. We can restore the original data set by using the restore command.


In the below example, we will explicitly employ the restore command to help us. First, upload the data set

sysuser auto.dta,clear

Now, we will preserve the data set by employing the preserve command

preserve  collapse (mean) price mpg weight, by(foreign)  restore  list in 1/2

This result shows the original data set.

Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x