SASxport (version 1.7.0)

write.xport: Write Data to a SAS XPORT File

Description

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

Usage

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
 )

Arguments

One or more data frames to be stored

list

A list containing data frames to be stored.

file

File name or connection object. Use "" to view the raw data

verbose

Logical flag controlling whether status is reported during processing

sasVer

SAS version string

osType

Operating system, defaults to "R X.Y.Z" for appropriate values of X, Y, and Z

cDate

Date object specifying dataset creation date

formats

Optional data frame containing SAS format information.

autogen.formats

Logical indicating whether SAS formats should be auto-generated for factor variables.

Value

No return value

Details

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.

References

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

Examples

Run this code
# NOT RUN {
#####
## 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, '*'),
  stringsAsFactors=TRUE
  )

## look at it
abc

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

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

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

## verify the additions
str(abc)

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

# list the contents of the file
lookup.xport(tmp)

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

## and look at it
xxx.abc

## Check the label and type
label(xxx.abc)
SAStype(xxx.abc)

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

# }

Run the code above in your browser using DataLab