Calling R Functions from SAS with PROC IML

  • Slides: 31
Download presentation
Calling R Functions from SAS… …. with PROC IML Ph. USE 2011 – 12.

Calling R Functions from SAS… …. with PROC IML Ph. USE 2011 – 12. 10. 2011 Dr. Peter Bewerunge © 2009 HMS Analytical Software Gmb. H

Company • HMS Analytical Software is a specialist for Information Technology in the field

Company • HMS Analytical Software is a specialist for Information Technology in the field of Data Analysis and Business Intelligence Systems • Profile – 40 employees in Heidelberg, Germany – SAS Institute Partner for 15 years – Doing data oriented software projects for more than 20 years – Focus on life science industry • Technologies – Analytics and Data Management: SAS, JMP, R, Microsoft SQL Server – Application Development: Microsoft. NET, Java Dr. Peter Bewerunge © 2009 HMS Analytical Software Gmb. H 2

Our IT Services for the Life Science Industry (SAS, JMP, R and Microsoft) Independent

Our IT Services for the Life Science Industry (SAS, JMP, R and Microsoft) Independent Consulting Programming Data Management Data Mining / Analysis Training and Individual Coaching • Application Development and Integration • Software Validation • • • Dr. Peter Bewerunge © 2009 HMS Analytical Software Gmb. H 3

Agenda • • Servey Who needs an interface to R Requirements How to submit

Agenda • • Servey Who needs an interface to R Requirements How to submit R Code? What about data sets and SAS formats? Do I get my R Graphics into SAS-Report How to handle errors coming from R Questions that came up during Ph. USE Dr. Peter Bewerunge © 2009 HMS Analytical Software Gmb. H 4

Degree of Popularity of R compared to Ph. USE 2010 yes no Who knows

Degree of Popularity of R compared to Ph. USE 2010 yes no Who knows R? Who uses R? 2% 30% 70% 98% Who would like to use R in the validated environment? Who uses R in the validated environment? 50% Person 1 0 Dr. Peter Bewerunge © 2009 HMS Analytical Software Gmb. H 5

The Best of two Worlds Dr. Peter Bewerunge © 2009 HMS Analytical Software Gmb.

The Best of two Worlds Dr. Peter Bewerunge © 2009 HMS Analytical Software Gmb. H 6

The Best of two Worlds Client Tier Management Console Data Integration Studio Information Map

The Best of two Worlds Client Tier Management Console Data Integration Studio Information Map Studio Enterprise Guide …. Web Tier Web Report Studio Information Delivery Portal STP Web Application …. Metadata Server Tier Connect Server SAS Foundation OLAP Server STP Server …. Bio. Conductor Bioinformatic community Rgeo Spatial Statistics g. R Graphical Models Data Tier Data Sets OLAP Cubes ERP data structure RDBMS Tables …. Robus statistics Rmetrics Financial Market Analysis Omega Distributed Statistical Computing Dr. Peter Bewerunge © 2009 HMS Analytical Software Gmb. H 7

Who needs an Interface to R? • People who don´t want a “religious” decision

Who needs an Interface to R? • People who don´t want a “religious” decision between SAS or R. They want both! • People who want to use one of the huge amount of add-on-packages of R • Lazy people, who don´t want to convert their existing R code to SAS • People who switch from R to SAS • People who want to validate their SAS programs (Nikhil Abhyankar, yesterday talk) Dr. Peter Bewerunge © 2009 HMS Analytical Software Gmb. H 8

SAS/IML as an Interface to R • SAS Interactive Matrix Language (IML) is a

SAS/IML as an Interface to R • SAS Interactive Matrix Language (IML) is a programming language for explorative data analysis. • wide range of most standard matrix operations • direct access to SAS data sets • create dynamic graphics it has the an interface to R PROC IML use this interface Dr. Peter Bewerunge © 2009 HMS Analytical Software Gmb. H 9

Requirements to Use PROC IML as an Interface to R 32 -bit and 64

Requirements to Use PROC IML as an Interface to R 32 -bit and 64 -bit Windows 32 -bit and 64 -bit Linux 32 -bit version of R 64 -bit version of R Supported R Versions SAS 9. 22 x x R-2. 11. 1 or before SAS 9. 3 x x ? ? • R must be installed on the same computer / server • access to the SAS workspace server via Enterprise Guide® Dr. Peter Bewerunge © 2009 HMS Analytical Software Gmb. H 10

How to submit R Code 1. Start SAS with the RLANG-option (C: . .

How to submit R Code 1. Start SAS with the RLANG-option (C: . . . sas. exe -RLANG). 2. Check access to R NORLANG: ERROR: WARNING: RLANG: Do not support access to R language interfaces The RLANG system option must be specified in the SAS configuration file or on the SAS invocation command line to enable the submission of R language statements. SAS option RLANG is not supported on this host. Support access to R language interfaces Dr. Peter Bewerunge © 2009 HMS Analytical Software Gmb. H 11

How to submit R Code 3. Use PROC IML as the interface to R

How to submit R Code 3. Use PROC IML as the interface to R Dr. Peter Bewerunge © 2009 HMS Analytical Software Gmb. H 12

Sending Data from SAS to R and back - IML built-in subroutines. From SAS

Sending Data from SAS to R and back - IML built-in subroutines. From SAS to R: Subroutine Export. Data. Set. To. R Export. Matrix. To. R SAS Source SAS data set SAS/IML matrix R Destination R data frame R matrix R Source SAS Destination From R to SAS: Subroutine Import. Data. Set. From. R R expression Import. Matrix. From. R R expression Dr. Peter Bewerunge © 2009 HMS Analytical Software Gmb. H SAS data set SAS/IML matrix 13

Sending SAS Data Sets to R Dr. Peter Bewerunge © 2009 HMS Analytical Software

Sending SAS Data Sets to R Dr. Peter Bewerunge © 2009 HMS Analytical Software Gmb. H 14

Getting SAS Data Sets back from R Dr. Peter Bewerunge © 2009 HMS Analytical

Getting SAS Data Sets back from R Dr. Peter Bewerunge © 2009 HMS Analytical Software Gmb. H 15

What about Date, Times and Datetime Values? From SAS to R: SAS Date (e.

What about Date, Times and Datetime Values? From SAS to R: SAS Date (e. g. DATEw. d) Datetime (e. g. DATETIMEw. d) R Class „Date“ Classes that inherit from Class „POSIXT“ Time (e. g. TIMEw. d) All other formats Classes inherit from Class „POSIXT“ Class „numeric“ Dr. Peter Bewerunge © 2009 HMS Analytical Software Gmb. H 16

What about Date, Times and Datetime Values? From R to SAS: R Class „Date“

What about Date, Times and Datetime Values? From R to SAS: R Class „Date“ Class „POSIXT“ In all other cases Dr. Peter Bewerunge © 2009 HMS Analytical Software Gmb. H SAS DATE 9. TIME 19. no SAS format is assigned 17

Get Graphics from R to SAS Dr. Peter Bewerunge © 2009 HMS Analytical Software

Get Graphics from R to SAS Dr. Peter Bewerunge © 2009 HMS Analytical Software Gmb. H 18

Get Graphics from R to SAS Dr. Peter Bewerunge © 2009 HMS Analytical Software

Get Graphics from R to SAS Dr. Peter Bewerunge © 2009 HMS Analytical Software Gmb. H 19

Handling Errors from R Error: Division by a alphanumeric variable: 1. have a look

Handling Errors from R Error: Division by a alphanumeric variable: 1. have a look to the SAS LOG Dr. Peter Bewerunge © 2009 HMS Analytical Software Gmb. H 20

Handling Errors from R 2. read out the global SAS macro variable SYSERRORTEXT (holds

Handling Errors from R 2. read out the global SAS macro variable SYSERRORTEXT (holds the last error message) Dr. Peter Bewerunge © 2009 HMS Analytical Software Gmb. H 21

Handling Errors from R 3. Use try. Catch-block in R and write the error

Handling Errors from R 3. Use try. Catch-block in R and write the error message to a SAS data set Dr. Peter Bewerunge © 2009 HMS Analytical Software Gmb. H 22

Handling Errors from R 3. Use try. Catch-block in R and write the error

Handling Errors from R 3. Use try. Catch-block in R and write the error message to a SAS data set Dr. Peter Bewerunge © 2009 HMS Analytical Software Gmb. H 23

Limitations • No option to tell SAS which R to use. • Large data

Limitations • No option to tell SAS which R to use. • Large data sets are limited by memory size – In R the data are load and processed in the main memory. The same is true for SAS IML. • R comes along with absolutely no warranty – take this especially into account for the large amount of add-on-packages. • Not all data types from R can be converted – every data type has to be converted either to a data. frame or a matrix before transferring data to SAS Dr. Peter Bewerunge © 2009 HMS Analytical Software Gmb. H 24

Questions during Ph. USE… • What is faster SAS or R? Dr. Peter Bewerunge

Questions during Ph. USE… • What is faster SAS or R? Dr. Peter Bewerunge © 2009 HMS Analytical Software Gmb. H 25

Questions during Ph. USE… • Is PROC IML the only way to run R

Questions during Ph. USE… • Is PROC IML the only way to run R programs? Dr. Peter Bewerunge © 2009 HMS Analytical Software Gmb. H 26

Questions during Ph. USE… • Is R easy to learn? Dr. Peter Bewerunge ©

Questions during Ph. USE… • Is R easy to learn? Dr. Peter Bewerunge © 2009 HMS Analytical Software Gmb. H 27

Questions during Ph. USE… • Is R easy to learn? 2 -day-training course, HMS

Questions during Ph. USE… • Is R easy to learn? 2 -day-training course, HMS Dr. Peter Bewerunge © 2009 HMS Analytical Software Gmb. H 28

Questions during Ph. USE… Is are a validated environment? • no, it is NOT:

Questions during Ph. USE… Is are a validated environment? • no, it is NOT: R comes along with absolutely no warranty Dr. Peter Bewerunge © 2009 HMS Analytical Software Gmb. H 29

Conclusion • The best of two worlds. • SAS implements a powerful interface to

Conclusion • The best of two worlds. • SAS implements a powerful interface to R within PROC IML. • SAS data sets can be send to R and back. • Some SAS formats are converted to R counterpart. • R graphics can be integrated into SAS PDF-reports. • Different ways to handle errors from R in SAS. Dr. Peter Bewerunge © 2009 HMS Analytical Software Gmb. H 30

Thank you for your Attention Dr. Peter Bewerunge Software Engineer HMS Analytical Software Gmb.

Thank you for your Attention Dr. Peter Bewerunge Software Engineer HMS Analytical Software Gmb. H Rohrbacher Str. 26 • 69115 Heidelberg Telefon +49 6221 6051 -0 Peter. Bewerunge@analytical-software. de www. analytical-software. de Dr. Peter Bewerunge © 2009 HMS Analytical Software Gmb. H 31