2

I am writing some code in Stata and I have already used preserve once. However, now I would like to preserve again, without using restore.

I know this will give an error message, but does it save up to the new preserve area?

Tommy Atlee
  • 23
  • 1
  • 4

1 Answers1

5

No, preserving twice without restoring in-between simply throws an error:

sysuse auto, clear

preserve
drop mpg

preserve
already preserved
r(621);

However, you can do something similar using temporary files. From help macro:

"...tempfile assigns names to the specified local macro names that may be used as names for temporary files. When the program or do-file concludes, any datasets created with these assigned names are erased..."

Consider the following toy example:

tempfile one two three

sysuse auto, clear
save `one'

drop mpg
save `two'

drop price
save `three'

use `two'
list price in 1/5

     +-------+
     | price |
     |-------|
  1. | 4,099 |
  2. | 4,749 |
  3. | 3,799 |
  4. | 4,816 |
  5. | 7,827 |
     +-------+

use `one'
list mpg in 1/5

     +-----+
     | mpg |
     |-----|
  1. |  22 |
  2. |  17 |
  3. |  22 |
  4. |  20 |
  5. |  15 |
     +-----+