Read and Write 'SAS' 'XPORT' Files

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.

Project Status: Active - The project has reached a stable, usable state and is being actively developed. Linux Build Status Windows Build status CRAN RStudio mirror downloads Coverage Status

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.




From GitHub




Writing SAS XPORT files

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. 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 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.

Reading SAS XPORT files

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, or chron objects.
  • SAS labels are stored in label attributes on each variable, and are accessible using the label function.
  • SAS formats are stored in SASformat attributes on each variable, and are accessable using SASformat.
  • SAS iformats are stored in SASiformat attributes on each variable, and are accessable using SASiformat.
  • SAS integer variables are stored as integers unless force.integer is FALSE.

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


Version 1.6.0 -- 2018-02-23

Bug fixes:

  • write.xport() no longer sets an 8 character minumum length on string variables.

Other changes:

  • SAS-generated example xport files have been refreshed and some have modified to include string variables with less than 8 characters.
  • SAS code to generate all example datasets is now included.
  • The manual page for the 'Alfalfa' dataset now properly attributes the source of the data to Brian Yandell's book Practical Data Analysis for Designed Experiments.
  • Minor improvements to settings for automatic code testing & test coverage.

Version 1.5.7 - 2018-02-03

  • Add automated testing via TravisCI (for Linux) and AppVeyor (for Windows)
  • Add code coverage info via
  • Update & fix badges in
  • Minor reformatting of

Version 1.5.6 - 2018-02-02

  • Remove package load message.

Version 1.5.5 - 2018-02-01

Version 1.5.4 - 2016-03-25

Other changes:

  • Updated nchar() calls to specify type="byte" for clarity.

Version 1.5.0 - 2014-07-21

Bug fixes:

  • Now works properly on big-endian systems such as the PowerPC, Spark. (Reported by Brian Ripley )

  • Explicitly cast left bit shifts to avoid undefined C language behavior. (Reported by Brian Ripley)

  • Resolve problem in accessing "Hmisc::label.default<-" if SASxport::read.xport is called without loading SASxport. (Reported by Dominic Comtois)

  • 'read.xport' now preserves '$' at the beginning of SAS character format and iformat strings. (Reported by Dominic Comtois)

  • 'read.xport' argument names.tolower was not being honored for dataset names. (Reported by Dominic Comtois)

Other changes:

  • Modified several test files to display generated .xpt data so that issues can be more easily detected and diagnosed.

  • C code cleanup and reorgaization to improve clarity.

Version 1.4.0 - 2014-04-09

API Change:

  • SASxport now relies on the 'label' methods defined by the Hmisc package instead of defining its own.

Bug fixes:

  • The 'read.xport' and 'write.xport' functions were failing when both the SASxport and the Hmisc packages were loaded due to conflicts between the label methods defined by each package. This has been resolved by removing the label methods from SASxport and using those from Hmisc instead.

Version 1.3.6 2013-10-09

Bug fixes:

  • In manual pages for read.xport() and lookup.xporT(): Update URL for 'test2.xpt', and use a local copy for executed example code.

Version 1.3.5 2013-06-14

Bug fixes:

  • read.xport() and write.xport() now properly handle empty dataset/dataframe objects.

Version 1.3.4 2013-05-31

Bug fixes:

  • Correct error in write.xport when a factor contains only NA entries.

Other Changes:

  • Package test scripts now use a fixed timezone to prevent unhelpful warnings.

Version 1.3.2 2013-05-11

New features:

  • dataset label and type are now supported. See write.xport() and read.xport() for examples.

Bug fixes:

  • Integrate patch from foreign package to properly handle xport files with datasets that end exactly on an 80-byte record boundry & add corresponding test file.

  • Replace file.path(path.package(...)) with system.file(...)


  • Remove oboslete .First.lib() function

  • Replace file.path(path.package(...)) with system.file(...)

Version 1.3.1 2013-03-24


  • Replace use of depreciated .path.package() with path.package() for R 3.0.0.

Version 1.3.0 2012-06-27

New features:

  • New function makeSASNames() to create valid and unique SAS names from character vectors.

Bug fixes:

  • Improper handling of duplicates names in write.xport() was generating names longer than 8 characters, resulting in invalid files. Corrected by using the new makeSASNames() function instead of the R make.names() function.

Version 1.2.4 2010-11-11

Bug fixes:

  • Fix bug in handling of '' argument to read.xport ('' was not operating as documented).

Version 1.2.3 2008-02-29

Bug fixes:

  • Fix typo in manual page for write.xport() reported by Yinlai Meng.

Version 1.2.2 2007-11-09

Bug fixes:

  • Apply patches to fix problems on 64 bit platforms, as sumitted by Brian Ripley.

Version 1.2.1 2007-11-05


  • Correct warning message due to extraneous ';' charcters after function closing braces.

Version 1.2.0 2007-11-01

New Features:

  • SAS format and iformat information is now accessed via 'SASformat()' and 'SASiformat()' functions instead of 'formats' and 'iformat'. The information accessed by these functions is now stored in attributes with the same name. This avoids conflicts with the use of 'format' by chron objects.

  • Copies of the code for foreign::read.xport and foreign::lookup.xport is now part of the SASxport package, permitting extension to these functions as needed, and removing the dependency on the foreign package.

  • Overflow of SASxport numeric format values, which have a smaller range than IEEE 754 numeric values now standard, now generates NAN instead of 0.0.

Bug Fixes:

  • Fix for problem storing negative numbers.

  • SAS format length and digit information is now properly captured by read.xport(). This is supported by an improved version of lookup.xport().

  • SAS format information was not being properly utilized when more than one format was present.

  • Improved handling of SAS date formats


  • Test routines added to test handling of numeric values.

Version 1.1.1

  • Display support information at package startup

Version 1.1.0 -

New Features:

  • Add support for autogeneration of SAS FORMAT information as a PROC CONTENTS fmtin= dataset. This enables R factors to be handled properly on the receiving system.

Bug Fixes:


Version 1.0.0

New Fetures:

Bug Fixes:


Beta 3 - 2007-08-10

New features:

  • read.xport's names.tolower argument now defaults to FALSE so that variable (and data set) names are now left as uppercase.

  • Improved crediting of BRL-CAD source code

Bug fixes:

  • Correct call to sprintf where printf was intended in src/ieee2ibm.c


  • Augmented ieee2ibm code with corresponding ibm2ieee code for completeness.

Beta 2 - 2007-08-10

New Features:

  • Replaced IEEE to IBM translation code with GPL'ed version from BPL-CAD.

Bug Fixes:

  • Changes to C code should correct the C99 usage errors

  • Correct documentation typos, including those reported by Tim.

Beta 1 - 2007-08-10

Initial version of the SASxport package.

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.


1.7.0 by Gregory R. Warnes, 2 years ago

Report a bug at

Browse source code at

Authors: Gregory R. Warnes <[email protected]> -- Unless otherwise noted , the contents of this package were written by Gregory R. Warnes <[email protected]> and are provided under the terms of the GNU General Public License , version 2.0 or later. -- The files 'src/ieee2ibm.c' and 'src/ibm2ieee.c' were extracted from BRL-CAD file /brlcad/src/libbu/htond.c written by Michael John Muuss , Copyright (c) 2004-2007 United States Government as represented by the U.S. Army Research Laboratory , and is utilized and redistributed under the terms of the GNU Lesser General Public License , version 2.1. -- The file 'R/read.xport.R' is adapted from the 'Hmisc' package created by Frank E. Harrell , Jr. <[email protected]> , and is utilized and redistributed under the terms of the GNU General Public License , version 2.0 or later. -- The files 'R/xport.R' , 'src/SASxport.c' , 'src/SASxport.h' , and 'src/foreign.h' are copied or adapted from the 'R' 'foreign' package created by Douglas M. Bates <[email protected]> and Saikat DebRoy <[email protected]> , and are utilized and redistributed under the terms of the GNU General Public License , version 2.0 or later. -- The creation of this package was partially funded by Metrum Institute <>.

Documentation:   PDF Manual  

GPL-2 license

Imports Hmisc, utils, stringi

Enhances chron

Imported by define.

Suggested by datetime.

See at CRAN