Learn R Programming

SASxport (version 1.7.0)

toSAS.default: Convert R Data Object for Storage in a SAS XPORT File

Description

The toSAS methods control how R objects and data types are represented when stored into a SAS xport format file using write.xport.

Usage

toSAS(x, format, format.info=NULL)
# S3 method for default
toSAS(x, format=SASformat(x), format.info=NULL)
# S3 method for numeric
toSAS(x, format=SASformat(x), format.info=NULL)
# S3 method for logical
toSAS(x, format=SASformat(x), format.info=NULL)
# S3 method for character
toSAS(x, format=SASformat(x), format.info=NULL)
# S3 method for factor
toSAS(x, format=SASformat(x), format.info=NULL)
# S3 method for POSIXt
toSAS( x, format="DATETIME16.", format.info=NULL)
# S3 method for Date
toSAS(x, format="DATE9.", format.info=NULL)

Arguments

x

Object to be converted

format

SAS format name

format.info

Table of SAS format information

Value

A vector of type "character" or of type "numeric", with an attribute named "label" containing the SAS format specification.

Details

To add support for a new object type, create an appropriate toSAS method. This method must convert the object data to either an object of type "numeric" (double-precision floating point) or type "character", the only basic types permitted by the xport format, and should add an attribute named "SASformat" to the object providing an appropriate SAS format string or "" (indicating the default SAS format).

See Also

write.xport, read.xport, lookup.xport

Examples

Run this code
# NOT RUN {
####
## See how an R date/time object will be stored in a SAS xport file:
####

# Date and time
dateTimeObj <- ISOdate(2007,08,01,10,14,37)
class(dateTimeObj)
dateTimeObj

sasDateTimeObj <- toSAS(dateTimeObj)
sasDateTimeObj

# Now just the date portion
dateObj <- as.Date(dateTimeObj)
dateObj

sasDateObj <- toSAS(dateObj)
sasDateObj

####
## Create a new R object class based on factor to hold color names
####
colorFactor <- function(x) # constructor
  {
    retval <- factor(x, levels=c("Red","Green","Blue") )
    class(retval) <- c("colorFactor","factor")
    retval
  }

## create one and look at it
cf <- colorFactor( c("Red","Red","Blue",NA) )
cf

## See how it will be represented in a SAS xport file
toSAS(cf)

## Create a new conversion function to store as a RGB hex value
toSAS.colorFactor <- function(x, format="")
{
   retval <- ifelse(x=="Red", "#FF0000",
                    ifelse(x=="Green", "#00FF00", "#0000FF") )
   attr(retval, "SASformat") <- format
   retval
}

## see it in action
toSAS(cf)

# }

Run the code above in your browser using DataLab