Download presentation
Presentation is loading. Please wait.
1
%SUBMIT R A SAS® Macro to Interface SAS and R
Ross Bettinger Consultant
2
Agenda Motivation %SUBMIT_R and SAS/IML
Description of %SUBMIT_R operation Examples R Installation and Interface With SAS
3
Motivation to write %SUBMIT_R macro
R Project for Statistical Computing is open-source software, freely downloadable from Object-oriented functional programming language with excellent data handling features Large number of packages that provide wide variety of analysis techniques and graphics Steep learning curve due to its complexity and variety Open-source Minimal cost, e.g., $0 No one source of technical support equivalent to SAS Tech Support. Must search on Internet for answers, a very rich source of information. To make SAS and R work together, one must Use SAS’ superior data manipulation capabilities to process data Export a SAS dataset as a delimited text file Use R to import the delimited text file Use R to manipulate data and produce superior graphics A unified approach is to avoid manual steps 2 and 3 by invoking R from within SAS and returning graphical results in the SAS viewer
4
%SUBMIT_R follows SAS/IML structure
SAS/IML can call functions written in R and return results from R functions Structure of SAS/IML—R interface PROC IML ; submit / r ; f1 <- 1 ; f2 <- 1 ; f3 <- f1 + f2 ; f4 <- f2 + f3 ; f5 <- f3 + f4 ; cat( "The first five Fibonacci numbers are", c( f1, f2, f3, f4, f5 ), "\n" ) endsubmit; /* note: no space between “endsubmit” and “;” (SAS 9.3) */ QUIT ; Result of execution: The first five Fibonacci numbers are
5
%SUBMIT_R follows SAS/IML structure (cont’d)
%SUBMIT_R equivalent to SAS/IML code Can use %R() macro as auxiliary tool to create R-language input %R( f1 <- 1 ; f2 <- 1 ; f3 <- f1 + f2 ; f4 <- f2 + f3 ; f5 <- f3 + f4 ; cat( "The first five Fibonacci numbers are", c( f1,f2,f3,f4,f5 ), "\n" ) ) %SUBMIT_R %SUBMIT_R macro searches macro environment for global macro variable &R_SCRIPT created by %R() and transmits &R_SCRIPT contents to R for execution
6
%SUBMIT_R follows SAS/IML structure (cont’d)
SAS/IML can transmit data to and from R environment PROC IML ; call ExportDataSetToR( "Sashelp.Class", "class_df" ) ; submit / R ; class_df.names <- names( class_df ) call ImportDataSetFromR( "Work.class_df_names", "class_df.names" ) endsubmit ; QUIT ; PROC PRINT data=Work.class_df_names ; run ; Output: class_ Obs df_names Name Sex Age Height Weight
7
%SUBMIT_R follows SAS/IML structure (cont’d)
%SUBMIT_R equivalent of SAS/IML submit/r statement: %R( ExportDataSetToR( Sashelp.Class, class_df ) ; class_df.names <- names( class_df ) ; ImportDataSetFromR( WORK.class_df_names, class_df.names ) ; ) %SUBMIT_R PROC PRINT data=Work.class_df_names ; run ; Output Obs x Name Sex Age Height Weight
8
%SUBMIT_R(SAS Data) R Graphics
%R( ExportDataSetToR( Sashelp.Class, class_df ) ; Model <- lm( class_df$Weight ~ class_df$Height, na.action="na.exclude" ) ; ParmEst <- coef ( Model ) ; Pred <- fitted ( Model ) ; Resid <- residuals( Model ) ; png( "C:/Users/UserID/Documents/My SAS Files/SAS+R/SUBMIT_R/submit_r.png", bg="transparent" ); par( mfrow=c(2,2)) ; plot( class_df$Height, class_df$Weight, xlab="Height", ylab="Weight" ) ; title( "Weight vs Height" ) ; abline( ParmEst ) ; plot( class_df$Height, Pred, xlab="Height", ylab="Predicted Weight" ) ; title( main=paste(sep='', "Predicted Weight vs Height", "\n", "Correlation=" , format( cor( class_df$Height, Pred ), digits=2, nsmall=2 ))) ; plot( class_df$Height, Resid, xlab="Height", ylab="Residual" ) ; title( main="Residuals vs Height" ) ; hist( Resid ) ; dev.off() ) %SUBMIT_R
9
%SUBMIT_R(SAS Data) R Graphics (cont’d)
10
Using %SUBMIT_R to read R statements from a file
Contents of sombrero.R library( plot3D ) INCREM <- pi / 20 MULTIPLE <- 2 Y_LO <- X_LO <- - MULTIPLE * pi Y_HI <- X_HI <- MULTIPLE * pi elts <- seq( X_LO, X_HI, by= INCREM ) n_elts <- length( elts ) ^ 2 x <- matrix( t( replicate( n_elts, elts ))) y <- matrix( rep ( elts , n_elts )) xyz <- cbind( x, y, rep( NA, n_elts )) for( i in 1 : dim( xyz )[ 1 ] ) { xyz[ i, 3 ] <- sin( sqrt( xyz[ i, 1 ] ^ 2 + xyz[ i, 2 ] ^ 2 )) } scatter3D( xyz[, 1 ], xyz[, 2 ], xyz[, 3 ] ) title( main="3D Plot" ) %SUBMIT_R( RSCRIPT=sombrero.R )
11
sombrero <- function( x, y ) { sin( sqrt( x^2 + y^2 )) }
12
Using SAS/Macro with %SUBMIT_R
Generate sombrero plot using SAS/Macro to pass parameter values %R( library( plot3D ) ; INCREM <- pi / 20 ; Y_LO <- X_LO <- -&MULTIPLE * pi ; Y_HI <- X_HI <- &MULTIPLE * pi ; n_elts <- length( elts <- seq( X_LO, X_HI, by= INCREM )) ; x <-matrix( t( replicate( n_elts, elts ))) ; y <-matrix( rep( elts, n_elts )) ; xyz <- cbind( x, y, rep( NA, n_elts )) ; sombrero <- function( x, y ) { sin( sqrt( x^2 + y^2 )) } ; for( i in 1:dim( xyz )[1] ) { xyz[i, 3] <- sombrero( xyz[i, 1], xyz[i, 2] ) } ; scatter3D( xyz[, 1 ], xyz[, 2 ], xyz[, 3 ], type="l" ) ; title( main="Sombrero" ) ; ) %let MULTIPLE = 1.5 ; %SUBMIT_R
13
Sombrero
14
%SUBMIT_R operation R code from %R() or input file is written to R input file Unnamed pipe statement is used to spawn a process that will invoke R executable Results of execution are written to SAS log or user-specified output file Graphics are written to HTML file, ODS is used to display image in Results Viewer window
15
R Installation and Interface With SAS
Download appropriate R executable from Interface R with SAS via autoexec.sas file, e.g., "C:\Program Files\SASHome\SASFoundation\9.3\sas.exe" ^ -autoexec "C:\Users\UserName\Documents\My SAS Files\autoexec.sas“ Set R_HOME environmental variable in autoexec.sas: Options set=R_HOME=“C:\Program Files\R\R " ; Must use SAS-compatible version of R See for compatibility table
16
References R Core Team (2015). R: A language and environment for statistical computing. R Foundation for Statistical Computing, Vienna, Austria. URL project.org/. SAS/IML 12.1 Online Help, “Using R to Analyze Data in SAS/IML Matrices” SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. Other brand and product names are trademarks of their respective companies SAS Tech Support document
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.