Workflow
2026-03-028 min read

Stata preserve/restore and tempvar: Safe Data Manipulation Patterns

preserve and restore let you make destructive changes safely. tempvar and tempfile prevent namespace pollution. Here are the patterns every do-file needs.

Sytra Team
Research Engineering Team, Sytra AI

You need a temporary collapse or merge, but one wrong step could overwrite the only in-memory version of your cleaned dataset.

You will apply safe transformation patterns that let you test operations without risking irreversible data loss.

All examples tested in Stata 18 SE. Compatible with Stata 15+.


Quick Answer

  1. Use preserve before destructive operations.
  2. Restore as soon as temporary computation is complete.
  3. Use tempvar/tempfile for intermediate objects in reusable scripts.
  4. Avoid hard-coded temporary variable names.

Make Destructive Steps Reversible by Default

Wrap destructive transforms with preserve and restore

Analytic scripts often need temporary subsetting or aggregation. preserve/restore provides a transaction-like pattern for these operations.

This keeps your main dataset intact while still enabling intermediate computations.

If you are extending this pipeline, also review Export Regression Tables in Stata and Clustered Standard Errors in Stata.

preserve-restore.do
stata
1clear all
2set obs 1200
3gen firm_id = ceil(_n/12)
4gen year = 2014 + mod(_n,10)
5gen wage = 18 + rnormal(0,4)
6gen education = 8 + floor(runiform()*10)
7
8count
9preserve
10 keep if year >= 2018
11 collapse (mean) mean_wage=wage mean_edu=education, by(year)
12 list year mean_wage mean_edu
13restore
14count
. count
  1200
๐Ÿ’กRestore immediately after temporary block
Keep preserved scopes short. Long preserved blocks are harder to debug and increase risk of state confusion.

Use tempvar and tempfile to prevent namespace collisions

Reusable do-files and programs fail when temporary variable names clash with existing columns. tempvar prevents this by generating safe names.

tempfile is equally useful when temporary disk artifacts are needed across commands.

tempvar-tempfile.do
stata
1clear all
2set obs 1200
3gen firm_id = ceil(_n/12)
4gen year = 2014 + mod(_n,10)
5gen wage = 18 + rnormal(0,4)
6gen education = 8 + floor(runiform()*10)
7
8count
9preserve
10 keep if year >= 2018
11 collapse (mean) mean_wage=wage mean_edu=education, by(year)
12 list year mean_wage mean_edu
13restore
14count
15
16* ---- Section-specific continuation ----
17tempvar z_wage
18quietly summarize wage
19gen `z_wage' = (wage - r(mean)) / r(sd)
20
21tempfile subset
22preserve
23 keep firm_id year `z_wage'
24 save `subset'
25restore
26
27use `subset', clear
28describe
. describe
Contains data from /var/folders/.../ST_00001.tmp
 Observations:         1,200
 Variables:                3
โš ๏ธTemporary names are session-specific
Do not copy tempvar names into external scripts. They are generated dynamically each run.

Common Errors and Fixes

"nothing to restore"

restore was called without an active preserved dataset state.

Ensure preserve is executed in the same run path before restore.

. restore
nothing to restore
r(622);
This causes the error
wrong-way.do
stata
restore
This is the fix
right-way.do
stata
preserve
keep if year>=2018
restore
error-fix.do
stata
1capture noisily preserve
2keep if year>=2018
3restore
. restore
. restore

Command Reference

preserve / restore / tempvar

Stata docs โ†’

Supports reversible dataset operations and collision-safe temporary objects.

preserve ... restore ; tempvar tname ; tempfile fname
preserveSaves in-memory dataset state
restoreRestores preserved state
tempvarCreates guaranteed-unique temporary variable names
tempfileCreates temporary file handles for disk-backed steps

How Sytra Handles This

Sytra can wrap risky transformations with preserve/restore and manage tempvar/tempfile lifecycle automatically.

A direct natural-language prompt for this exact workflow:

sytra-prompt.txt
bash
Wrap a temporary collapse and subset operation with preserve/restore, use tempvar for standardized wage, save an intermediate tempfile, then restore original data and verify row count unchanged.

Sytra catches these errors before you run.

Sytra can wrap risky transformations with preserve/restore and manage tempvar/tempfile lifecycle automatically.

Join the Waitlist โ†’

FAQ

When should I use preserve and restore?

Use them around destructive operations like collapse, keep/drop, or temporary merges when you need to return to the original dataset state.

Why use tempvar instead of regular variable names?

tempvar avoids naming collisions in reusable programs and automatically cleans up after execution.

Can I nest preserve calls?

Stata supports one active preserved dataset at a time; nested preserve logic needs careful sequencing or tempfiles.


Written by Sytra Team
Research Engineering Team, Sytra AI

We build practical, reproducible workflows for Stata and R teams working on real empirical research pipelines.

#Stata#preserve#tempvar#Programming

Enjoyed this article?