Export Stata data to CSV
How Stata's export delimited command turns a dataset into CSV or TSV, what it keeps and drops, and how to check the file stays usable after sharing.
Exporting a Stata dataset to CSV turns a typed, labeled .dta file into plain delimited text that almost any program can open. That portability is the main benefit. The cost is that much of what makes a Stata dataset self-describing does not travel with it. This review explains what the export changes, what it leaves behind, and how to check whether the result is fit for its intended use. It relies on Stata's documented behavior and general file-format principles. No version-specific test results were supplied, so confirm details against the help file for your own installation.
Is CSV the right format for your purpose?
CSV is a good choice when the goal is to move values into a spreadsheet, a database, another statistics package, or a script, and the receiver does not need Stata's metadata. The decisive tradeoff is that a CSV file carries values only. It does not carry variable labels, value label definitions, storage types, display formats, notes or the distinction between different kinds of missing data. If the recipient also uses Stata, sharing the .dta file is usually the better option, since it preserves all of that.
- CSV fits well when:
- the recipient uses software other than Stata
- the data must be readable or checkable in a text editor
- a pipeline or upload form requires delimited text
- you can send variable and label documentation separately
- Consider another route when:
- the recipient uses Stata (share the .dta file)
- the data depends on extended missing codes such as .a or .b
- value labels must stay attached to their numeric codes in one file
- the main audience will open the file in a spreadsheet that may silently reformat IDs, long numbers or dates
Within CSV itself there is a second choice: export value labels as text, or export the underlying numeric codes. Labeled text is easier for a person to read. Numeric codes are better for analysis in another program, as long as a codebook goes with them.
The basic command
Since Stata 13, the standard command is export delimited. Older code may use outsheet, which does similar work. The command writes the dataset in memory, or a selected subset of it, to a text file.
* Export all variables to a comma-separated file, overwriting any existing copy
export delimited using "mydata.csv", replace
* Export selected variables and observations
export delimited id age income using "subset.csv" if year == 2020, replace
* Tab-separated output instead of commas
export delimited using "mydata.tsv", delimiter(tab) replace
* Semicolon delimiter, for recipients whose locale uses a decimal comma
export delimited using "mydata.csv", delimiter(";") replace
Several documented options change what ends up in the file:
| Option | Effect |
| replace | Overwrites an existing file of the same name |
| delimiter(tab) or delimiter("char") | Uses a tab or another character instead of a comma |
| nolabel | Writes numeric codes instead of value-label text |
| datafmt | Writes numeric variables using their display formats instead of raw values |
| novarnames | Leaves out the header row of variable names |
| quote | Puts quotation marks around all string values |
What the export changes and what it drops
The export does not restyle or reorder characters. It converts Stata's internal storage into text. Each observation becomes a line and each variable becomes a field. Text in string variables, including non-English characters, is written out as characters rather than transformed. The main changes concern how numbers and labels are represented and which metadata is lost.
| Stata element | What happens in the CSV |
| Variable names | Written as the header row unless novarnames is used |
| Variable labels | Not included |
| Value labels | Written as label text by default; numeric codes with nolabel |
| Value label definitions | Not included, so the code-to-text mapping is lost |
| Storage types (byte, float, double, str#) | Not included; the importing program guesses types again |
| Display formats | Ignored by default; applied with datafmt |
| System missing (.) | Written as an empty field |
| Extended missing (.a to .z) | No longer distinguishable as separate codes in plain CSV; verify with your own data |
| Notes and characteristics | Not included |
Value labels versus numeric codes
By default, a labeled numeric variable is written as its label text. In Stata's bundled auto dataset, for example, the variable foreign is stored as 0 or 1 with the labels Domestic and Foreign. A default export writes the words. With nolabel, it writes the numbers. The words are clearer for a human reader, but another program will read them as text categories. The numbers are compact and easy to analyze, but meaningless without the mapping. If you export codes, save the label definitions separately, for example with label save, or share a codebook.
* Export numeric codes and keep a record of the label definitions
export delimited using "mydata_codes.csv", nolabel replace
label save using "mydata_labels.do", replace
Display formats, dates and precision
Stata separates a stored value from the way it is shown on screen. By default, export delimited writes the unformatted value. The datafmt option applies each variable's display format instead, which can round or pad numbers. That is useful for presentation but can discard precision. Dates are a common source of confusion because Stata stores them as numbers counted from a base date and displays them through a format. Check how your date variables appear in the exported file. If the output is ambiguous, create an explicit string version first, for example with the string() function and a date format, so the recipient gets an unambiguous date.
Variables stored as float hold limited precision, which can show up as unexpected trailing digits or small differences when values are written out. If exact values matter, compare a reimported copy against the original rather than assuming they match.
Will the file stay usable after sharing?
Delimited text is broadly compatible, but how a file is opened matters as much as how it was written. These issues come from general behavior of text files and spreadsheet software. How they play out depends on the recipient's program, version and settings, and no scoped tests of specific destinations were supplied for this review.
- Encoding: Stata 14 and later handle text as Unicode (UTF-8). Some spreadsheet programs may misread non-ASCII characters in a UTF-8 file depending on how it is opened. If accented or non-Latin text matters, open the file in the target program before distributing it.
- Delimiters and locale: where a comma is the decimal separator, a comma-delimited file may split into the wrong columns. A tab or semicolon delimiter can avoid this.
- Automatic reformatting: spreadsheets often convert fields that look like numbers or dates. Leading zeros in ID codes can disappear and long digit strings can be rounded or shown in scientific notation. Store such identifiers as strings in Stata, and warn recipients not to save the file back over the original after opening it in a spreadsheet.
- Quoting: string values containing the delimiter must be quoted. The quote option quotes all strings, which is safer when free text contains commas, quotation marks or line breaks.
- Header row: dropping variable names with novarnames makes the file harder to interpret. Keep the header unless a receiving system specifically rejects it.
On accessibility: a CSV file is plain text, so it does not depend on fonts or styling. How well it works with a screen reader depends on the program used to open it. Reading raw comma-separated lines aloud is very different from navigating a spreadsheet grid. No documentation or testing with specific assistive technologies was supplied, so do not assume a particular experience. When accessibility matters, readable label text, clear variable names and an accompanying codebook will help most readers.
Checking the result
The most reliable check is to read the file back in and compare it with the source. A numeric-code export is easier to compare than a labeled one, because labeled variables come back as strings.
* Save the original, export, then reimport and compare
save "original.dta", replace
export delimited using "check.csv", nolabel replace
import delimited using "check.csv", clear
describe
cf _all using "original.dta", verbose
Differences reported by cf point to variables worth inspecting, such as dates, float values, missing codes or identifiers whose type changed on import. Also open the CSV in the program your recipient will actually use, since a correct file can still be displayed misleadingly.
Gaps in this review
No subject-specific evidence was supplied: no test files, no version comparisons and no measurements from particular spreadsheet or assistive-technology setups. The behavior described here follows Stata's documented options and general properties of delimited text. Option names and defaults can vary between Stata releases. Check the help file for export delimited in your installation, and verify date, precision and encoding handling with a sample of your own data before relying on the output.