6 import delimited — Import and export delimited text data
datafmt specifies that all variables be exported using their display format. For example, the number
1000 with a display format of %4.2f would export as 1000.00, not 1000. The default is to use
the raw, unformatted value when exporting.
quote specifies that string variables always be enclosed in double quotes. The default is to only
double quote strings that contain spaces or the delimiter.
replace specifies that filename be replaced if it already exists.
Remarks and examples stata.com
Remarks are presented under the following headings:
Introduction
Importing a text file
Using other delimiters
Specifying variable types
Exporting to a text file
Video example
Introduction
import delimited reads into memory a text file in which there is one observation per line and
the values are separated by commas, tabs, or some other delimiter. The two most common types of
text data to import are comma-separated values (.csv) text files and tab-separated text files, often
.txt files. import delimited will automatically detect either a comma or a tab as the delimiter.
Similarly, export delimited writes Stata data to a text file. By default, export delimited
uses a comma as the delimiter, but you may specify another delimiter.
Imported string data containing ASCII or UTF-8 will always display correctly in the Data Editor and
Results window. Imported string data containing extended ASCII may not display correctly unless you
specify the character encoding using the encoding() option to convert the extended ASCII to UTF-8.
Exported text files are UTF-8 encoded.
If you are not sure that import delimited will do what you are looking for, see [D] import and
[U] 22 Entering and importing data for information about Stata’s other commands for importing
data.
Importing a text file
Suppose we have a .csv data file such as the following auto.csv, which contains variable names
and data for different cars.
. copy https://www.stata.com/examples/auto.csv auto.csv
. type auto.csv
make,price,mpg,rep78,foreign
"AMC Concord",4099,22,3,"Domestic"
"AMC Pacer",4749,17,3,"Domestic"
"AMC Spirit",3799,22,,"Domestic"
"Buick Century",4816,20,3,"Domestic"
"Buick Electra",7827,15,4,"Domestic"
"Buick LeSabre",5788,18,3,"Domestic"
"Buick Opel",4453,26,,"Domestic"
"Buick Regal",5189,20,3,"Domestic"
"Buick Riviera",10372,16,3,"Domestic"
"Buick Skylark",4082,19,3,"Domestic"
We would like to import these data into Stata for subsequent analysis.