toSAS.default            package:SASxport            R Documentation

_C_o_n_v_e_r_t _R _D_a_t_a _O_b_j_e_c_t _f_o_r _S_t_o_r_a_g_e _i_n _a _S_A_S _X_P_O_R_T _F_i_l_e

_D_e_s_c_r_i_p_t_i_o_n:

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

_U_s_a_g_e:

     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)

_A_r_g_u_m_e_n_t_s:

       x: Object to be converted 

  format: SAS format name

format.info: Table of SAS format information

_D_e_t_a_i_l_s:

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

_V_a_l_u_e:

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

_A_u_t_h_o_r(_s):

     Gregory R. Warnes greg@random-technologies-llc.com

_S_e_e _A_l_s_o:

     'write.xport', 'read.xport', 'lookup.xport'

_E_x_a_m_p_l_e_s:

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

