Write Data to a SAS XPORT File

This function writes one or more data frames into a SAS XPORT format library file.

write.xport(..., list=base::list(), file = stop("'file' must be specified"), verbose=FALSE, sasVer="7.00", osType, cDate=Sys.time(), formats=NULL, autogen.formats=TRUE )
One or more data frames to be stored
A list containing data frames to be stored.
File name or connection object. Use "" to view the raw data
Logical flag controlling whether status is reported during processing
SAS version string
Opererating system, defaults to "R X.Y.Z" for appropriate values of X, Y, and Z
Date object specifying dataset creation date
Optional data frame containing SAS format information.
Logical indiciating whether SAS formats should be auto-generated for factor variables.

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. If autogen.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 provided below.)

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.


No return value


This package was created with partial funding by Metrum Institute http://metruminstitute.org.


SAS Technical Support document TS-140: ``The Record Layout of a Data Set in SAS Transport (XPORT) Format'' available at https://support.sas.com/techsup/technote/ts140.pdf

See Also

toSAS, lookup.xport, read.xport, label, SAStype, SASformat, and SASiformat

  • write.xport

## R version of the example given in TS-140

## manually create a data set
abc <- data.frame( x=c(1, 2, NA, NA ), y=c('a', 'B', NA, '*' ) )

## look at it

## add a format specifier (not used by R)
SASformat(abc$x) <- 'date7.'

## add a variable label (not used by R)
Hmisc::label(abc$y) <- 'character variable'

## add a dataset label and type
Hmisc::label(abc) <- 'Simple example'
SAStype(abc) <- 'MYTYPE'

## verify the additions

# create a SAS XPORT file
tmp <- tempfile(fileext = ".dat")
write.xport( abc, file = tmp )

# list the contents of the file
## Not run: 
# lookup.xport(tmp)
# ## End(Not run)

## reload the data
xxx.abc <- read.xport(tmp)

## and look at it

## Check the label and type

## Note that the variable names and SAS dataset type have been converted
## to uppercase

Documentation reproduced from package SASxport, version 1.5.3, License: GPL-2

Community examples

Looks like there are no examples yet.