Read data files from SAS (.sas7bdat, .xpt), Stata (.dta) and SPSS
(.sav, .zsav, .por) into a Polars DataFrame, like haven reads
them into tibbles. You can read only some columns or a range of rows, and
choose whether labelled values come back as their labels or as the stored
codes.
read_readstat() will detect the format from the file extension, while
read_dta(), read_sas(), read_xpt(), read_sav() and read_por() read
the specified format.
Usage
read_readstat(
path,
col_select = NULL,
skip = 0,
n_max = NULL,
value_labels = TRUE,
empty_string_as_null = TRUE,
user_na = FALSE,
missing_codes = FALSE,
catalog_file = NULL,
encoding = NULL,
threads = NULL,
format = NULL
)
read_dta(path, ...)
read_sas(path, ...)
read_xpt(path, ...)
read_sav(path, ...)
read_por(path, ...)
read_stata(path, ...)
read_spss(path, ...)Arguments
- path
Path to the file.
- col_select
Character vector of column names to read, in the order they should appear.
NULL(default) reads all columns.- skip
Number of rows to skip before reading.
- n_max
Maximum number of rows to read.
NULL(default) reads all rows.- value_labels
If
TRUE(default), labelled values are returned as their labels; ifFALSE, the stored codes are kept. See Details.- empty_string_as_null
If
TRUE(default), empty strings are read as null; ifFALSE, they are kept as"". See Details.- user_na
Only for SPSS files. If
TRUE, SPSS user-defined missing values are kept as values; ifFALSE(default), they are read as null. See Details.- missing_codes
Not for SAS transport and SPSS portable files.
TRUEto add a column of missing value codes after each column that can have special or user-defined missing values (numeric Stata and SAS columns, SPSS columns with user-defined missing values), a character vector of column names to add one after those columns only, orFALSE(default) for none. Can't be used withuser_na = TRUE. See Details.- catalog_file
Only for SAS files. Path to a SAS format catalog (
.sas7bcat) holding the value labels of a SAS data file, orNULL(default). See Details.- encoding
Only for Stata files. The text encoding of a Stata file, as a label such as
"latin1","GBK"or"UTF-8", orNULL(default) to use the format's: UTF-8 from Stata 14 on, Windows-1252 before. See Details.- threads
Number of decoding threads.
NULL(default) picks one per physical core.- format
One of
"sas","xpt","stata","spss"(.sav,.zsav) or"por"(SPSS portable).NULL(default) detects it from the extension.- ...
Passed on to
read_readstat().
Details
Column types
| Stored in the file | Polars type |
Stata byte / int / long | Int8 / Int16 / Int32 |
Stata long in files older than format 113 | Int64 |
Stata float / double | Float32 / Float64 |
| SAS and SPSS numbers | Float64 |
| Strings, including Stata strLs | String |
Dates: SAS date formats, Stata %td, SPSS date formats | Date |
Stata %tw, %tm, %tq, %th, %ty | Date, the first day of the period |
Datetimes: SAS datetime formats, Stata %tc/%tC, SPSS DATETIME | Datetime in UTC |
Times of day: SAS time formats, SPSS TIME | Time |
SPSS DTIME | Duration |
Datetimes are stored as clock times without a time zone. They are returned
in UTC, like haven, so the clock time stays the same whatever R's time
zone is. Their unit is milliseconds, or microseconds for SAS. Fractional
seconds are kept. A Stata %tc column is a datetime even when its display
format shows only the time of day.
In Stata files older than format 113, long -2147483648 is a valid value,
not a missing one. R uses that number for NA_integer_, so these columns
are Int64, which keeps the value when converted to R.
Missing values
Numeric missing values become null: system missing values, and the
special missing values .a to .z in Stata and .A to .Z and ._ in
SAS.
SPSS user-defined missing values (discrete values and ranges, for numbers
and strings) also become null by default. Set user_na = TRUE to keep them
as values, like haven::read_sav(user_na = TRUE); with value_labels = TRUE, a kept missing value that has a label becomes its label.
Set missing_codes to also learn which missing value each null was. Each
column it names gets a String column <name>_missing right after it,
holding the code of each special or user-defined missing value: ".a" in
Stata, ".A" in SAS, and the value itself in SPSS (e.g. "-99"). It is
null where the value isn't missing or is a system missing value. This is
what haven keeps in tagged NAs (Stata, SAS) and with user_na = TRUE (SPSS). read_readstat_metadata() lists each variable's missing
values and the labels of their codes (missing_labels for Stata and SAS,
value_labels for SPSS).
These formats have no missing value for strings and store an empty string
instead, so empty strings are read as null by default. Set
empty_string_as_null = FALSE to keep them as "", like haven does.
Strings of spaces are data and are always kept.
Value labels
With value_labels = TRUE, a column that has value labels becomes a
String column. Labelled values are replaced by their labels, and values
without a label are written out as text (2 becomes "2"), like
haven::as_factor().
SAS keeps value labels in a separate format catalog (.sas7bcat). Pass it
as catalog_file to label SAS columns: a column is labelled when the
catalog has a format of the same name as the column's display format, as in
haven::read_sas(). Without a catalog, SAS columns keep their codes.
Text encoding
Text is returned as UTF-8. SAS and SPSS files, and Stata files from Stata 14
on (format 118 and later), record their encoding. Older Stata files don't:
they hold text in the code page of the system that wrote them, read as
Windows-1252 (Latin-1, the code page of Western Windows systems) as in
haven, ReadStat and pandas. For a file written elsewhere, e.g. on a
Chinese or Japanese system, set encoding to its code page ("GBK",
"Big5", "Shift_JIS", ...). Some programs write UTF-8 into old formats
(e.g. haven::write_dta(version = 13)): if accented letters come out as
pairs of odd characters, use encoding = "UTF-8". Bytes that aren't valid
in the encoding become the replacement character (U+FFFD).