Preserve & restore

Commands like collapse change the data irreversibly. If you would like to continue working on your previous data afterwards, you need to preserve your data before you run any irreversible commands. Then, you can restore your initial data.

Preserve & restore data

***************************
*** preserve & restore  ***


/*
"preserve" saves the current data, such that you can run commands which change
the current data, and "restore" brings back the initial data. You need to run
all lines from "preserve" to "restore" at once.
*/


sysuse census, clear 

preserve 			//save data in the background as is
	drop if region==3
	/*do analysis
	.
	.
	.
	*/
restore				//bring me back to the data version saved with preserve

// very useful in combination with collapse

sysuse census, clear 
preserve
	collapse (count) state_num = pop (mean) pop* (max) pop_max=pop, by(region)
	su
	list region pop pop_max
restore
// Also see: frames