Initializing playground…
← Back to roadmap

📊 readStata & toStata — Interactive Playground

Read and write Stata DTA files from TypeScript. toStata(df) serializes a DataFrame to a Stata DTA v118 binary buffer. readStata(buf, options) parses the buffer back into a DataFrame. Numeric missing values are represented as null. Mirrors pandas.read_stata() and DataFrame.to_stata().
Edit any code block below and press ▶ Run (or Ctrl+Enter) to execute it live in your browser.

1 · Basic round-trip — write and read back

Create a DataFrame, serialize it to a Stata DTA v118 binary buffer with toStata(), then parse it back with readStata(). All columns, values, and shape are preserved.

TypeScript
Click ▶ Run to execute

2 · Missing values — null round-trip

Stata represents missing numeric values as special sentinel bit patterns. readStata maps all missing sentinels to null. toStata writes the standard Stata system-missing value for each type.

TypeScript
Click ▶ Run to execute

3 · Options — dataLabel & variableLabels

Embed a dataset description with dataLabel and per-column annotations with variableLabels. These metadata fields are stored in the DTA header and are visible in Stata's describe command.

TypeScript
Click ▶ Run to execute

4 · Options — usecols, nRows, indexCol

Restrict columns with usecols, limit rows with nRows, and promote a column to the DataFrame index with indexCol.

TypeScript
Click ▶ Run to execute

5 · Boolean columns

Boolean values are stored as Stata byte (int8) with true → 1 and false → 0. Reading converts them back to numbers; use .map() or comparison operators to recover booleans if needed.

TypeScript
Click ▶ Run to execute

6 · writeIndex — include the row index

Pass writeIndex: true to include the DataFrame's row index as an extra _index column in the DTA file.

TypeScript
Click ▶ Run to execute