dta
A pure Rust library for reading and writing Stata's DTA file format.
DTA is the binary format used by Stata to persist datasets. It's still the dominant interchange format in academic economics, public health, and social science research — often the only format that downstream collaborators can actually open. This library provides a streaming reader and writer covering every released version of the format (104 through 119), including XML-framed releases (117+), tagged missing values, value-label sets, and long-string (strL) storage.
The API is built around a typestate chain, so you can't accidentally write a schema before a header, or read data bytes before the schema has been parsed. Each phase borrows the underlying I/O handle and hands it to the next phase, keeping the whole pipeline zero-copy where possible.
Usage
Reading a file
The reader walks through the sections of a DTA file in order. Each phase returns the next phase when you're done with it.
use DtaReader;
use Result;
Each reader exposes header() and schema() accessors, so you can inspect metadata without threading them through yourself.
Lazy records
RecordReader::read_record() eagerly decodes every cell in the row. If you only need a subset of columns — or you want to defer parsing until you know whether you care — use read_lazy_record() instead. LazyRecord hands back raw byte slices that you decode one cell at a time.
while let Some = record_reader.read_lazy_record?
Writing a file
The writer is the mirror image of the reader: a typestate chain that locks in the ordering of the header, schema, characteristics, data, long strings, and value labels. The <map> offsets and the header's K/N placeholders are patched in place as each section closes, which is why the writer requires Write + Seek.
use ByteOrder;
use DtaWriter;
use Header;
use Release;
use Schema;
use Value;
use Variable;
use VariableType;
use StataDouble;
use StataLong;
use Result;
RecordWriter::write_record validates arity and per-column types against the schema, so a mismatch is caught at the call site rather than producing a malformed file.
Feature Flags
tokio — async I/O
Disabled by default. Enable it for async reading and writing with tokio.
[]
= { = "0.1", = ["tokio"] }
= { = "1", = ["fs", "io-util", "rt", "macros"] }
This unlocks:
DtaReader::from_tokio_path,from_tokio_file, andfrom_tokio_reader, returning anAsyncHeaderReaderthat mirrors the sync chain with.awaitat each stepDtaWriter::from_tokio_path,from_tokio_file, andfrom_tokio_writer, returning anAsyncHeaderWriter
The async writer requires AsyncWrite + AsyncSeek + Unpin so that the XML <map> and header placeholders can still be patched in place.
use DtaReader;
use Result;
async
Character encoding
encoding_rs is a hard dependency, not a feature flag. The reader and writer pick an encoding from the release number by default: Windows-1252 for pre-118 files and UTF-8 for 118+. If you need to override that — for example, reading a pre-118 file produced by a locale that wasn't Windows-1252 — pass an explicit encoding:
use DtaReader;
let reader = new
.encoding
.from_path?;
Benchmarks
The project ships Criterion benchmarks that exercise sync and async read/write throughput against a synthetic panel-style file:
# Sync benchmarks only (no optional features)
# Include the async (tokio) benchmarks
Results are saved to target/criterion/ with HTML reports. To run a single benchmark by name:
The first run generates benches/data/bench_large.dta (~100K rows) and caches it for subsequent runs.
Profiling
The repository includes a profiling workflow built on samply. One-time setup:
# Install samply (Linux / macOS / Windows)
# On Linux, samply needs perf_event_open access:
|
Then run the profiling script:
# Default: 1M records, top 10 functions
# Customize record count and report depth
On Windows, use .\profile.ps1 with the same options (-Records, -Top). samply uses ETW on Windows, so the script must run from an elevated PowerShell window.
The script builds optimized binaries with debug symbols, records separate samply profiles for the sync write, sync read, async write, and async read phases, and prints a table of the hottest functions from this crate by inclusive and self time.
About
This is a passion project that I maintain on my own time. I care deeply about its quality and want it to be genuinely useful, but I also want to keep it fun and sustainable. To that end:
- Bug reports are always welcome. Please file issues for anything that isn't working correctly.
- Feature requests are best expressed as pull requests. I'm much more likely to engage with a well-crafted PR than a request for new work.
- Timelines are my own. I'll get to things when I can, and I may close issues or PRs that don't align with the project's direction – nothing personal.
If you find this library valuable, the best way to support it is to contribute or share it with others.