Import CSV Files into Stata

How Stata's import delimited command turns CSV and TSV text into a dataset, what changes during import, and the checks that keep IDs, dates and text intact.

Importing a CSV file into Stata converts delimited plain text into a typed dataset that Stata can analyze. That conversion is useful, but it is not neutral. Column headers become variable names, digits become numbers, and text is read through a particular character encoding. This review covers what the import changes, where meaning can be lost, and when the original plain-text file should remain the reference copy.

Is importing the CSV the right step?

For analysis in Stata, yes. The modern route is the import delimited command. It reads comma- and tab-separated files and creates variables with appropriate storage types, so you can summarize, merge and model the data immediately. The main tradeoff is that Stata infers each column's type. That inference can silently change values whose meaning depends on their exact characters, such as ZIP codes, account numbers with leading zeros, very long identifiers and dates written as text.

Plain text is still the better format in some situations. Use the CSV when the data has to be shared with people who do not use Stata, archived in a software-neutral form, or kept as the untouched source of record. A sensible workflow keeps the original CSV unchanged, imports it with a do-file, and treats the resulting Stata dataset as a derived working copy.

The basic command

import delimited using "survey.csv", clear

The clear option replaces any dataset already in memory. Without it, Stata refuses to load the file if unsaved data is present. If the file is not in the current working directory, give the full path or change directory first with cd. By default, Stata tries to determine from the file whether it is comma- or tab-delimited and whether the first row holds variable names.

Automatic detection is convenient, but stating the important settings explicitly makes the import reproducible and easier to audit:

import delimited using "survey.csv", varnames(1) delimiters(",") encoding("utf-8") clear

For a tab-separated file, use delimiters("\t"). For semicolon-separated files, which are common where the comma is the decimal separator, use delimiters(";"). If you prefer menus, the File menu has an Import submenu with a delimited text option. It shows a preview before loading and records the equivalent command in the Results window, which you can copy into a do-file.

What the import actually changes

The CSV file on disk is not modified. Stata reads it and builds a dataset in memory, and nothing is written until you run save. The changes happen in how the text is interpreted:

Element | In the CSV | After import | What to watch
Column headers | Free text on the first line | Stata variable names, lowercase by default; names that are not valid in Stata are adjusted | Spaces, punctuation, leading digits and duplicate headers produce altered names; use case(preserve) to keep capitalization
Numeric-looking columns | Sequences of characters | Numeric variables; decimals use the default storage type unless asdouble is specified | Precision loss in long numbers and identifiers
Codes with leading zeros | Kept exactly as typed | Leading zeros dropped if the column is read as numeric | ZIP codes, FIPS codes, product and account codes
Dates and times | Text | Usually imported as strings | Must be converted before use as dates
Empty fields | Nothing between delimiters | Numeric missing (.) or empty string | Distinguish true blanks from placeholder text such as NA
Quoted fields | Quotes protect embedded delimiters and line breaks | Quotes removed, contents kept as one value | Malformed quoting can split or merge rows
Non-ASCII characters | Bytes in some encoding | Text interpreted using the encoding option | Garbled accented letters indicate an encoding mismatch

When a header has to be changed to form a valid variable name, Stata typically keeps the original header text as the variable label. Run describe after import to confirm what was kept.

Keeping identifiers and codes intact

The most common way meaning is lost is a code column being read as a number. A ZIP code of 02134 becomes 2134, and a long account number can be rounded if it is stored in a type without enough precision. Neither change triggers an error. To prevent this, tell Stata which columns to keep as text:

import delimited using "clients.csv", stringcols(1 4) clear

The stringcols option takes column positions in the file, not variable names. A cautious alternative is stringcols(_all), followed by destring on only the columns that are genuinely numeric. For decimal measurements where precision matters, the asdouble option stores floating-point columns as double instead of the default type.

Converting dates

Dates in a CSV are plain text, so Stata usually imports them as strings. Convert them with a date function and a mask that matches the order of the source text, then apply a display format:

rename visitdate visitdate_str
generate visitdate = date(visitdate_str, "YMD")
format visitdate %td
count if missing(visitdate) & !missing(visitdate_str)

The final count is the important check. Any value the mask cannot parse becomes missing without an error. A nonzero count means some source dates use a different layout. For timestamps, use clock() with a datetime display format; datetimes need a double storage type to keep their precision.

Encoding and non-English text

Stata 14 and later handle Unicode text. The encoding option tells Stata how to interpret the file's bytes. UTF-8 is the usual choice for files exported from modern tools. Some spreadsheet programs save CSV files in a legacy regional encoding unless a UTF-8 option is chosen, and those files may need an encoding such as windows-1252 or latin1. Accented letters or symbols that appear garbled after import point to a mismatch. Reimport with the correct encoding rather than editing the damaged values.

Will the data stay usable after saving and sharing?

A saved Stata dataset keeps variable types, labels and formats, which the CSV cannot hold. It is less portable, though. Opening it requires Stata or software that reads its file format, and older Stata releases may not open files saved by newer ones. The saveold command writes a file for an earlier release. For recipients without Stata, export delimited writes the data back to plain text.

A round trip does not guarantee an identical file. Leading zeros reappear only if the column was kept as a string. Dates are exported according to their display format. Value labels may be exported as labels or as underlying codes depending on the options used. If the exact original text matters, share the original CSV alongside any derived file. A plain-text CSV also remains readable in ordinary text editors and spreadsheet programs, including with assistive technologies that work with those programs, which makes it the safer format for broad distribution.

Checks to run after every import

  • Run describe to confirm variable names, storage types and labels.
  • Compare the number of observations with the number of data rows in the source file, excluding the header.
  • Run list in 1/5 and compare the first rows with the raw file.
  • Use codebook or summarize to spot unexpected missing values, impossible ranges or numeric columns that should be text.
  • Confirm that code columns kept their leading zeros and full length.
  • Count date values that failed to convert.

Older commands and other sources

Older guides use insheet. That command was superseded by import delimited in Stata 13. It still exists for compatibility, but new work should use import delimited. If the data starts as an Excel workbook, import excel can read it directly. Be aware that a CSV which has been opened and resaved in a spreadsheet program may already have lost leading zeros or had dates reformatted before Stata ever sees it. Inspect the raw text when values look wrong.

Gaps in this review

No subject-specific test results were available for this review. It contains no performance measurements, file-size limits or version-by-version comparisons. Option names and defaults described here reflect Stata's documented command behavior, but details can differ between releases. Running help import delimited in your installed version is the authoritative check before relying on a particular option.