Submitting R statements is similar to submitting SAS statements. You use a SUBMIT statement, but add the R option: SUBMIT / R. All statements in the program prior to the next ENDSUBMIT statement are sent to R for execution.
The simplest program that calls R is one that does not transfer any data between the two environments. In the following program, SAS/IML is used to compute the product of a matrix and a vector. The result is printed. Then the SUBMIT statement with the R option is used to send an equivalent set of statements to R.
/* Comparison of matrix operations in IML and R */ print "---------- SAS/IML Results -----------------"; x = 1:3; /* vector of sequence 1,2,3 */ m = {1 2 3, 4 5 6, 7 8 9}; /* 3x3 matrix */ q = m * t(x); /* matrix multiplication */ print q; print "------------- R Results --------------------"; submit / R; rx <- matrix( 1:3, nrow=1) # vector of sequence 1,2,3 rm <- matrix( 1:9, nrow=3, byrow=TRUE) # 3x3 matrix rq <- rm %*% t(rx) # matrix multiplication print(rq) endsubmit;
Figure 11.1: Output from SAS/IML and R
---------- SAS/IML Results ----------------- q 14 32 50 ------------- R Results -------------------- [,1] [1,] 14 [2,] 32 [3,] 50
The printed output from R is automatically routed to the SAS/IML Studio output window, as shown in Figure 11.1. As expected, the result of the computation is the same in R as in SAS/IML.