SASxport (version 1.5.3)

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) "toSAS"(x, format=SASformat(x), format.info=NULL) "toSAS"(x, format=SASformat(x), format.info=NULL) "toSAS"(x, format=SASformat(x), format.info=NULL) "toSAS"(x, format=SASformat(x), format.info=NULL) "toSAS"(x, format=SASformat(x), format.info=NULL) "toSAS"( x, format="DATETIME16.", format.info=NULL) "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

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