Read and Write SAS XPORT Files
This package provides functions for reading, listing the contents of, and writing SAS xport format files. The functions support reading and writing of either individual data frames or sets of data frames. Further, a mechanism has been provided for customizing how variables of different data types are stored.
devtools::install_github("mangothecat/SASxport")
library(SASxport)
write.xport
writes one or more data frames into a SAS XPORT format
library file.
The function creates a SAS XPORT data file (see reference) from one or more data frames. This file format imposes a number of constraints:
- Data set and variable names are truncated to 8 characters and converted to upper case. All characters outside of the set A-Z, 0-9, and '_' are converted to '_'.
- Character variables are stored as characters.
- If
autogen.formats = TRUE
(the default), factor variables are stored as numeric with an appropriate SAS format specification. Ifautogen.formats = FALSE
, factor variables are stored as characters. - All numeric variables are stored as double-precision floating point values utilizing the IBM mainframe double precision floating point format (see the reference).
- Date and time variables are either converted to number of days since 1960-01-01 (date only), or number of seconds since 1960-01-01:00:00:00 GMT (date-time variables).
- Missing values are converted to the standard SAS missing
value
.
The SAS XPORT format allows each dataset to have a label and a
type (set via the label
and SAStype
functions). In addition,
each variable may have a corresponding label, display format, and
input format. To set these values, add the attribute label
,
SASformat
, or SASiformat
to individual data frame. These
attributes may be set using the label
, SASformat
, and
SASiformat
functions. (See examples in the package.)
The actual translation of R objects to objects appropriate for SAS
is handled by the toSAS
generic and associated methods, which
can be (re)defined by the user to provide fine-grained control.
read.xport
reads a file as a SAS XPORT format library and returns a list
of data frames:
- SAS date, time, and date/time variables are converted
respectively to
Date
, POSIX, orchron
objects. - SAS labels are stored in
label
attributes on each variable, and are accessible using thelabel
function. - SAS formats are stored in
SASformat
attributes on each variable, and are accessable usingSASformat
. - SAS iformats are stored in
SASiformat
attributes on each variable, and are accessable usingSASiformat
. - SAS integer variables are stored as integers unless
force.integer
isFALSE
.
If the file includes the output of PROC FORMAT CNTLOUT=
,
variables having customized label formats will be converted to
factor
objects with appropriate labels.
If a datasets in the original file has a label or type, these will
be stored in the corresponding label
and SAStype
attributes,
which can be accessed by the label
and SAStype
functions.
GPL 2.1 © Gregory R. Warnes, United States Government, Frank E. Harrell, Jr., Douglas M. Bates, Mango Solutions