IMPORTDATASETFROMR Call

CALL IMPORTDATASETFROMR( SAS-data-set, RExpr ) ;

You can use the IMPORTDATASETFROMR subroutine to transfer data from an R data frame to a SAS data set. It is easier to read the subroutine name when it is written in mixed case: ImportDataSetFromR.

The arguments to the subroutine are as follows:

SAS-data-set

is a literal string or a character matrix that specifies the two-level name of a SAS data set (for example, Work.MyData).

RExpr

is a literal string or a character matrix that specifies the name of an R data frame or, in general, an R expression that can be coerced to an R data frame.

You can call the subroutine provided that the following statements are true:

  1. The R statistical software is installed on the SAS workspace server.

  2. The SAS system administrator at your site has enabled the RLANG SAS system option. (See The RLANG System Option.)

The following statements create a data frame in R named RData and copy the data into Work.MyData. The SHOW CONTENTS statement is then used to display attributes of the Work.MyData data, which demonstrates that the data were successfully transferred.

proc iml;
submit / R;
z = c('a','b','c','d','e')
RData <- data.frame(x=1:5, y=(1:5)^2, z=z)
endsubmit;

call ImportDataSetFromR("Work.MyData", "RData");

use Work.MyData;
show contents;
close Work.MyData;

Figure 23.136 Contents of a SAS Data Set Created from R Data
DATASET : WORK.MYDATA.DATA                                                      
                                                                                
 VARIABLE                          TYPE  SIZE                                   
 --------------------------------  ----  ----                                   
 A                                 num      8                                   
 Y                                 num      8                                   
 Z                                 char     1                                   
                                                                                
Number of Variables   : 3                                                       
Number of Observations: 5                                                       
                                                                                

You can transfer data from a SAS data set into an R data frame by using the EXPORTDATASETTOR call. See Chapter 11, Calling Functions in the R Language, for details about transferring data between R and SAS software.

The names of the variables in the SAS data set are derived from the names of the variables in the R data frame. The following rules are used to convert an R variable name to a valid SAS variable name:

  1. If the name is longer than 32 characters, it is truncated to 32 characters.

  2. A SAS variable name must begin with one of the following characters: 'A'–'Z', 'a'–'z', or the underscore (_). Therefore, if the first character is not a valid beginning character, it is replaced by an underscore (_).

  3. A SAS variable name can contain only the following characters: 'A'–'Z', 'a'–'z', '0'–'9', or the underscore (_). Therefore, if any of the remaining characters is not valid in a SAS variable name, it is replaced by an underscore.

  4. If the resulting name duplicates an existing name in the data set, a number is appended to the name to make it unique. If appending the number causes the length of the name to exceed 32 characters, the name is truncated to make room for the number.