import
Reads a spreadsheet or a text file into memory.
What we have
import delimitedfor comma- and tab-separated textimport excel, includingfirstrow- SPSS
.savfiles, throughuse
Coming later
- fixed-format text (
infile,infix) - SAS files
- reading only some of the columns or rows
How you write it
import delimited using "file.csv" [, clear] | import excel using "file.xlsx" [, clear firstrow]
Square brackets mean optional. A vertical bar means choose one.
What it does
import delimited reads comma- or tab-separated text. import excel reads .xlsx and .xls; add firstrow when the top row of the sheet holds the column names rather than data.
This is usually the first command of a real piece of work, because most data arrives as a spreadsheet.
Watch out. A column that looks numeric but contains even one stray entry — a
-, an NA, a comma inside a number — arrives as text and will not do arithmetic. destring fixes that, and describe is how you spot it.Try it
Every example below was run after sysuse vira.sample02, clear.
. export delimited using "out.csv", replace
file out.csv saved
The same thing in pandas and in R
R
# no R form for `export` yet -- see docs/r-equivalents.md
. import delimited using "out.csv", clear
(6,000 observations read)
The same thing in pandas and in R
pandas
import pandas as pd
df = pd.read_csv("out.csv")
R
# no R form for `import` yet -- see docs/r-equivalents.md