*! version 1.0.0 15jul2026
help okpreserve VVK

Title

okpreserve/okrestore --Nested preserve and restore for data workflows

Syntax

okpreserve

okrestore

Description

okpreserve and okrestore provide a solution for managing
complex, multi-level data transformations in Stata. These commands enable
true nested preservation of data states, which is essential when your
analysis involves multiple stages of data aggregation, filtering, and
transformation.

Unlike Stata's built-in preserve/restore, which do not
support nesting, okpreserve/okrestore maintain a stack of
saved data states. This allows you to work with different levels of data
granularity within a single do-file.

Key Features

Nested Levels: Save and restore data at multiple levels simultaneously

Stack-Based: Each level is independent and fully preserves state

Memory Efficient: Uses tempfiles, no duplicate in-memory datasets

Easy to Use: Simple two-command workflow (okpreserve / okrestore)

Motivation

Complex econometric analyses often require working with data at multiple
levels of aggregation:

· Individual-level data

· Collapse to state-year for regression

· Further subset for heterogeneity checks

· Return to original for next analysis

okpreserve makes this workflow seamless and error-free.

Workflow

Use okpreserve before making changes to your data. Use okrestore
to restore to the most recently saved state.

Important: Each okpreserve increments the nesting level; each
okrestore restores and decrements. Use LIFO order (Last In, First Out).

Example 1: Single-Level Preserve and Restore

Preserve original data, filter, analyze, and restore:

. use auto.dta

. display "Original N = " _N

. okpreserve

. keep if price > 5000

. display "Filtered N = " _N

. summarize price

. okrestore

. display "Restored N = " _N

Expected Output:

Original N = 74

Filtered N = 37

Restored N = 74

Example 2: Two-Level Nested Preserve and Restore

Collapse to state-year level, then subset by year:

. use mydata.dta

. display "Level 0 (Individual): " _N

. okpreserve

. collapse (sum) income (mean) age, by(state year)

. display "Level 1 (Collapsed): " _N

. okpreserve

. keep if year > 2015

. regress income age

. eststo model_recent

. okrestore

. display "Back to Level 1: " _N

. okrestore

. display "Back to Level 0: " _N

Expected Output:

Level 0 (Individual): 50,000 obs

Level 1 (Collapsed): 200 obs

Restored to Level 1: 200 obs

Restored to Level 0: 50,000 obs

Example 3: Three-Level Nested Preserve and Restore

Full workflow: individual ® collapsed ® filtered ® cleaned

. use mydata.dta

. display "Level 0 (Original): " _N

. okpreserve

. collapse (mean) price mpg, by(foreign)

. display "Level 1 (Collapsed): " _N

. okpreserve

. keep if foreign == 1

. display "Level 2 (Foreign only): " _N

. okpreserve

. drop if missing(price)

. display "Level 3 (No missing): " _N

. summarize price mpg

. eststo foreign_clean

. okrestore

. display "Restored to Level 2: " _N

. okrestore

. display "Restored to Level 1: " _N

. okrestore

. display "Restored to Level 0: " _N

Expected Output Flow:

Level 0 (Original): 74 obs

Level 1 (Collapsed): 2 obs

Level 2 (Foreign = 1): 1 obs

Level 3 (No missing): 1 obs ® Analysis performed here

Restored to Level 2: 1 obs

Restored to Level 1: 2 obs

Restored to Level 0: 74 obs

Use Cases

Staggered Difference-in-Differences Analysis

· Load individual-level data with treatment assignment and outcomes

· Collapse to state-year level for main DiD regression

· Subset by demographic groups for heterogeneity analysis

· Return to original data for robustness checks

Multi-Stage Data Pipelines

· Load raw administrative data

· Clean and validate (preserve original safely)

· Aggregate to analysis level

· Conduct sensitivity analyses at different granularity levels

Technical Notes

okpreserve uses temporary files to store data snapshots. Each level
maintains its own state independently. The nesting level is tracked globally
as $vpres_cnt. Do not modify this global manually.

Author

Vikrant V. Kamble

Economics PhD Candidate, University of Delaware, USA

Former Short-Term Consultant, World Bank Group, Washington, DC

Email: vvkamble@udel.edu

Version 1.0.0 — July 15, 2026