write.xport: Write Data to a SAS XPORT File

Description Usage Arguments Details Value Note Author(s) References See Also Examples

View source: R/write.xport.R

Description

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

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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.

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:

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.

Value

No return value

Note

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

Author(s)

Gregory R. Warnes greg@warnes.net

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#####
## 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

warnes/SASxport documentation built on March 12, 2020, 7:27 p.m.