dodo dodo / dta
github
dodo / dta
duckdb community extension
dta
read and write Stata .dta files, natively in DuckDB.

dta is a DuckDB extension that reads and writes Stata .dta files — formats 117 through 121, corresponding to Stata 13–18 — with full type mapping, value labels, and strL support.

install in 10 seconds

dta is distributed through the DuckDB community extension repository. From any DuckDB session:

SQL// in any duckdb session
INSTALL dta FROM community;
LOAD dta;

usage

The extension adds the table function read_dta, registers itself as the default reader for .dta files, and enables writing tables to .dta with COPY.

SQL
-- Read a .dta file
SELECT * FROM read_dta('auto.dta');

-- Or let DuckDB infer the format from the file name
SELECT * FROM 'auto.dta';

-- Read with value labels converted to DuckDB ENUMs
SELECT * FROM read_dta('auto.dta', value_labels=true);

-- Write a table to .dta
COPY my_table TO 'output.dta' (FORMAT dta);

-- The default writer also infers .dta from the file name
COPY my_table TO 'output.dta';

why dta

Native, both directions
Read and write .dta inside DuckDB — no Stata installation, no Python round-trip, no CSV export in between.
Dates and datetimes
Stata %td and %tc formats convert to DuckDB DATE and TIMESTAMP — and back again on write.
Value labels
Pass value_labels=true to get labeled columns as DuckDB ENUMs. On write, ENUM columns become value labels again.
Long strings
Fixed-length strings and strL both read as VARCHAR; long strings write back as strL entries.

type mapping

reading — .dta to DuckDB

Stata typeDuckDB type
byteTINYINT
intSMALLINT
longINTEGER
floatFLOAT
doubleDOUBLE
double (%td)DATE
double (%tc)TIMESTAMP
strN, strLVARCHAR

writing — DuckDB to .dta

DuckDB typeStata type
BOOLEAN, TINYINTbyte
SMALLINTint
INTEGERlong
BIGINT, HUGEINTdouble
FLOATfloat
DOUBLE, DECIMALdouble
DATEdouble (%td)
TIMESTAMPdouble (%tc)
VARCHARstrL
ENUMbyte/int/long + value labels
output format

The writer produces format 119 (Stata 15), supporting up to 2 billion variables. When reading with value_labels=true, columns with value labels are returned as ENUM.

community extension duckdb.org / extensions / dta source · MIT license github.com/codedthinking/duckdb-dta