Stata Frames: Lightweight Reference Lookups Without Merging
Stata frames (since Stata 16) let you keep a small reference dataset in memory and copy variables into your analysis frame without merging. This blog shows a concrete pattern for country‑code lookups, the commands to set it up, and the trade‑offs versus merge and preserve/restore.
16 Oct 2026, 16:06 UTC

The lookup problem
Analysts often need a small, static table — country codes, industry classifications, or a custom mapping — for occasional value lookups during an analysis. The usual options each have drawbacks: merge expands the working dataset and modifies it permanently; preserve/restore blocks are verbose and nest poorly; saving and reloading a reference file on every lookup is slow and clutters the working directory.
Since Stata 16, frames let you keep several named datasets in memory simultaneously. Each frame holds its own variables, observations, and variable properties. You switch between frames with frame change and copy variables across frames with frame put and frame get. This pattern is ideal for read‑only reference data that you want to query without altering your main analysis dataset.
How frames work
A frame is created with frame create framename. You can load data directly into a frame using use filename, frame(framename) or switch to the frame first with frame change framename and then use. The default frame is named default. Variables are copied from a source frame into the current frame with frame get varlist from sourceframe, or pushed the other way with frame put varlist into targetframe. No row‑wise matching occurs; the variables are copied observation‑by‑observation in the current sort order, so you must ensure the frames are aligned (same number of observations, same sort) before copying.
Worked example: country‑code reference frame
Suppose you have an analysis dataset survey.dta in the default frame and a small reference file country_codes.dta with variables iso3 and country_name. You want to pull country_name into the analysis frame for labeling without merging.
* Run in a Stata do‑file or interactive session (Stata 16 or later)
* 1. Load the main data into the default frame
use "survey.dta", clear
* 2. Create a reference frame and load the code table
frame create ref_country
use "country_codes.dta", frame(ref_country)
* 3. Return to the analysis frame
frame change default
* 4. Ensure both frames are sorted identically on the key (iso3)
sort iso3
frame change ref_country
sort iso3
frame change default
* 5. Copy the variable from the reference frame
frame get country_name from ref_country
* 6. Verify the variable appeared
describe country_name
list iso3 country_name in 1/5
Where to run: Stata do‑file editor or interactive command window. Permissions: standard Stata user; no admin rights needed. Placeholders: replace survey.dta and country_codes.dta with your actual file paths. Expected checks: describe shows country_name with the correct storage type; list shows matched names for the first five observations. Risks: if the frames have different observation counts or sort orders, the copied values will be misaligned — always verify row counts with count in each frame before copying.
Trade‑offs and limitations
- No automatic joins: frames copy variables positionally. For large‑scale matching on keys,
merge(orjoinby) remains the correct tool. - Variable name conflicts: if the current frame already contains a variable named
country_name,frame getwill error. Rename or prefix variables in the reference frame before copying. - Memory‑only persistence: frames vanish on
clear,exit, or when Stata closes. Save any frame you need later withframe framename: save "filename.dta". - Memory footprint: each frame consumes RAM. Keep reference frames small; large reference tables may exceed available memory.
Quick verification checklist
- Run
aboutand confirm Stata version ≥ 16. - Run
frame dirto list existing frames. - After the example, run
frame diragain — you should seedefaultandref_country. - Use
frame change ref_countryfollowed bydescribeto confirm the reference frame is untouched.
If the checks pass, you have a reusable reference frame that stays in memory for the rest of the session — no repeated loads, no merges, no preserve/restore blocks.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.