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

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

View source: R/toSAS.R

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
toSAS(x, format, format.info=NULL)
## Default S3 method:
toSAS(x, format=SASformat(x), format.info=NULL)
## S3 method for class 'numeric'
toSAS(x, format=SASformat(x), format.info=NULL)
## S3 method for class 'logical'
toSAS(x, format=SASformat(x), format.info=NULL)
## S3 method for class 'character'
toSAS(x, format=SASformat(x), format.info=NULL)
## S3 method for class 'factor'
toSAS(x, format=SASformat(x), format.info=NULL)
## S3 method for class 'POSIXt'
toSAS( x, format="DATETIME16.", format.info=NULL)
## S3 method for class '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

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

Value

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

Author(s)

Gregory R. Warnes greg@warnes.net

See Also

write.xport, read.xport, lookup.xport

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

SASxport documentation built on March 13, 2020, 1:59 a.m.