help for writeinput

Advanced dataset-to-input command generator

writeinput
varlist
[if exp]
[in range]
[using newfile]
[,
replace
append
noclear
note(string)
header(string)
labels
dates
dryrun
markdown
precision(str)
maxobs(#)
sort(varlist)
sample(#)
seed(#)
varlab
generic
frame(name)]

Description

writeinput (v3.0.1) is a robust utility for generating reproducible Stata
input code from data currently in memory. It is specifically designed to
facilitate sharing "Minimum Working Examples" (MWEs) on forums like Statalist
or technical support emails.

Compared to version 1.x and 2.x, this version adds significant intelligence
regarding data precision (hexadecimal support), extended missing values (.a-.z),
and modern Stata 16+ features like frames.

Robustness & Correctness Options

precision(str) controls numeric precision. Use precision(hex)
to write double variables in hexadecimal format (`%21x`), ensuring
bit-for-bit exactness when the data is re-read. You can also pass standard
formats like precision(%20.0g).

Extended Missings: The command automatically detects and preserves
extended missing values (.a, .b, ..., .z).

String Escaping: All string values are automatically wrapped in
compound double quotes (`` `" "' ``), making the output
immune to corruption from internal double quotes or backticks.

Feature Options

maxobs(#) caps the number of rows written (default 500).
Set to 0 to disable the limit. This prevents accidental massive file creation.

varlab embeds variable labels as comments directly above the input line.

sort(varlist) sorts the observations by the specified variables
before writing, ensuring a consistent and logical order.

sample(#) and seed(#) allow you to write a
randomly drawn, reproducible subset of your data.

generic renames all output variables to v1, v2, ... to anonymize the
data structure while preserving the underlying logic.

markdown wraps the results window output in a fenced code block with
Stata syntax highlighting, ready for pasting into GitHub or Stack Overflow.

frame(name) allows you to pull data from a specific Stata 16+
frame without leaving your current workspace.

append allows adding the generated code to the end of an existing file.

Examples

High-precision export for technical support:

. writeinput mpg price weight, precision(hex) dryrun

Create an anonymized, sorted random sample for a forum post:

. writeinput age inc race, sample(20) seed(123) sort(age) generic markdown

Include variable metadata and a project header:

. writeinput *, varlab header("** Project X | Date: $S_DATE") dryrun

Returned Values

The command returns the following in r():

r(filename) The path to the created file

r(nobs) Number of observations written

r(nvars) Number of variables written

r(varlist) The list of variables written

r(truncated) 1 if maxobs was reached, 0 otherwise

Author

Eric A. Booth

eric.a.booth@gmail.com

http://www.eric-booth.com

Also see

On-line: help for input, dataex, frames