SlideShare a Scribd company logo
1 of 30
Download to read offline
SAS AND R AND SAS
Barry DeCicco
Ann Arbor Chapter, American Statistical
Association
May 22, 2018
CONTENTS
Calling R from SAS.
Calling SAS from R.
References.
CALLING R FROM SAS
USING SAS/IML
WHY USE R IF YOU HAVE SAS?
You can use SAS for what you already know well, and use
R only as needed.
You might find that you don’t have the capacities in SAS
which you desire.
R has vast and rapidly growing capacities – over 10,000
packages.
BASICS OF R
R must be installed on your computer. Download from
CRAN: https://cran.r-project.org
Necessary packages (modules) must be downloaded
from CRAN and installed: (select ‘Packages’ from the left-
hand menu).
You will need to set up SAS to use R.
SETTING UP SAS TO USE R
You need to edit the file ‘SASv9.cfg’, which controls
some configurations (run ‘proc options option=config;’
to find it).
Make a copy, and then edit the original, adding these
lines:
1. -RLANG
2. -config "C:Program
FilesSASHomeSASFoundation9.4nlsensasv9.cfg“[replace
with folder name for where this file is on your computer]
3. -SET R_HOME "C:Program FilesRR-3.5.0“ [replace with folder
name for where R lives on your computer]
HOW TO SUBMIT R CODE IN SAS
When running analyses/data manipulations in R, there
are things which need to be done:
Setting directories as needed, to pull/save data, output
and graph files.
Loading (already installed!) necessary packages for
use (e.g., ‘ggplot2’ for complex graphs).
These will need to be done in the SAS code
submitted to R each time.
STARTING LINES
Use PROC Options to enable SAS to use R:
proc options option=rlang; run;
Start IML (this has to be done through SAS/IML):
Proc IML;
Export SAS datasets to R as needed:
Call ExportDataSetToR(SAS-data-set, RDataFrame);
Let SAS know to start R:
submit / R;
ENDING LINES
Retrieve data sets from R as needed:
 CALL IMPORTDATASETFROMR (SAS-data-set, RExpr) ;
Let SAS know to end sending commands to R:
endsubmit;
Quit IML (if no further IML use is needed):
quit;
R LANGUAGE COMMANDS
Set the working directory (location where R retrieves and
stores files), while storing the previous location in ‘old_wd’:
 old_wd<-setwd("C:/Users/Barry/Desktop")
Resetting the working directory back to what it was:
setwd(old_wd);
# this is a comment
GOING THROUGH THE LOG -
PRELIMINARIES
 /* Start R options/ */
 proc options option=rlang;
 run;
 proc iml;
 title "Statistic in R (integration with SAS)";
 run ExportDataSetToR("WORK.RCBD_TEST", "RCBD_IN_R");
 submit / R;
GOING THROUGH THE LOG –
SUBMITTING COMMANDS TO R
 #_______Beginning of R code_________________
 # Change the working directory, saving the old one in ‘old_wd’
 old_wd<-setwd("C:/Users/Barry/Desktop")
 # Load the ‘qicharts’ module (package), so that it can be used.
 library(qicharts)
GOING THROUGH THE LOG –
SUBMITTING COMMANDS TO R
 # Create a data frame (data set)
 d2 <- data.frame(y = rnorm(144, 12, 3),
 expand.grid(x = seq.Date(as.Date('2014-1-1'),
 by = 'week',
 length.out = 24),
 g1 = LETTERS[1:3],
 g2 = letters[1:2]))
 # Introduce a shift in process performance
 d2$y[132:144] <- d2$y[132:144] * 3
GOING THROUGH THE LOG –
SUBMITTING COMMANDS TO R
 # Run a chart.
 trc(y ~ x | g1 + g2, data = d2, main = 'Trellis run chart', chart = 'i')
 # Direct the graphics output to a file, then close it.
 dev.copy(pdf,'myplot.pdf',width=6,height=4 )
 dev.off()
 # Reset the working directory
 setwd(old_wd)
GOING THROUGH THE LOG –
WRAPPING UP
 #_______End of R code_________________
 endsubmit; # Any error messages from R will now show up
 * Bring the data set from R to SAS;
 CALL IMPORTDATASETFROMR ("work.d2", "d2") ;
 proc print data=d2;
 run;
RESULT
l
CALLING SAS FROM R
USING KNITR AND
MARKDOWN
WHAT IS KNITR?
knitr is an R package which combines code,
comments, text and results into one
document.
You write a document with text and code
chunks.
knitr will run the code chunks and incorporate
the results, knitting them into a single
document.
This document can be in Word, HTML or PDF
(the latter requires outside software).
STARTING OFF – IN RSTUDIO
THREE OUTPUT OPTIONS
HTML – easiest to start
with.
Word.
PDF – this requires
external software to
finish.
I will use HTML
STARTING THROUGH THE DOCUMENT
Knitr will set up a
standard starting
document
prepopulated with
some items.
Title information.
Initial setup.
Explantion
CODE CHUNKS AND SETTING UP SAS
 Gray items are
‘code chunks’,
where there is code
to be executed.
 They start and end
with ```
 This sets up SAS in
this code chunk. It
tells R to use SAS,
and where SAS is.
LIMITATIONS
Each code chunk runs a separate batch job in
SAS. There is no hand-off of WORK. data sets
between SAS code chunks.
To hand off data sets (between SAS and SAS,
or SAS and R), save them as permanent files.
SAS HTML output plays well with knitr’s HTML
markdown. For other types of output, other
packages are needed.
Slashes/back-slashes in path names need to
be managed.
SET UP
Let R know that it’s using sas (the ‘SAS’ or
‘SASHTML’ engine).
Tell R where the SAS.exe file is.
Set any SAS options.
These can by done either by chunk, or overall
(for now, overall).
SET UP
There are at least 20 ‘engines’ available for
knitr, meaning 20 languages.
For SAS, there is the ‘sas’ and ‘sashtml’
engines.
For this presentation, the ‘sashtml’ engine will
be used.
SET UP
Once the set up
has been done
overall, then SAS
commands can
be done.
QUESTIONS?
REFERENCES – SAS
 SAS/IML(R) 9.3 User's Guide ‘The RLANG System Option’
http://support.sas.com/documentation/cdl/en/imlug/64248/HTM
L/default/viewer.htm#imlug_r_sect003.htm
 SAS/IML(R) 9.3 User's Guide‘Submit R Statements’
http://support.sas.com/documentation/cdl/en/imlug/64248/HTM
L/default/viewer.htm#imlug_r_sect004.htm
 SAS/IML(R) 12.1 User's Guide: EXPORTDATASETTOR Call
http://support.sas.com/documentation/cdl/en/imlug/65547/HTM
L/default/viewer.htm#imlug_langref_sect114.htm
 SAS/IML(R) 12.1 User's Guide: IMPORTDATASETFROMR Call
http://support.sas.com/documentation/cdl/en/imlug/65547/HTM
L/default/viewer.htm#imlug_langref_sect182.htm
REFERENCES – SAS (CON.)
 ‘Run R code inside SAS easily’ SAS/IML(R) 9.3 User's Guide
‘EXPORTDATASETTOR Call’
http://support.sas.com/documentation/cdl/en/imlug/65547/HTML/default
/viewer.htm#imlug_langref_sect114.htm
 SAS Blogs: Video: Calling R from the SAS/IML Language
https://blogs.sas.com/content/iml/2011/10/31/video-calling-r-from-the-
sasiml-language.html
REFERENCES - R
 ‘SAS Using R Markdown (Windows)’
https://www.ssc.wisc.edu/~hemken/SASworkshops/M
arkdown/SASmarkdown.html#html-or-pdf-document-
as-a-final-target
 The raw markdown version of the above page:
https://www.ssc.wisc.edu/~hemken/SASworkshops/M
arkdown/SASmarkdown.rmd
 ‘Tables and Graphs - Using the sashtml Engine’
https://www.ssc.wisc.edu/~hemken/SASworkshops/M
arkdown/SAShtmlengine.html
 Dynamic Documents with R and Knitr (by Yihui Xie)
https://yihui.name/knitr/

More Related Content

What's hot

MapReduce - Basics | Big Data Hadoop Spark Tutorial | CloudxLab
MapReduce - Basics | Big Data Hadoop Spark Tutorial | CloudxLabMapReduce - Basics | Big Data Hadoop Spark Tutorial | CloudxLab
MapReduce - Basics | Big Data Hadoop Spark Tutorial | CloudxLabCloudxLab
 
ACADILD:: HADOOP LESSON
ACADILD:: HADOOP LESSON ACADILD:: HADOOP LESSON
ACADILD:: HADOOP LESSON Padma shree. T
 
Introduction to MapReduce - Hadoop Streaming | Big Data Hadoop Spark Tutorial...
Introduction to MapReduce - Hadoop Streaming | Big Data Hadoop Spark Tutorial...Introduction to MapReduce - Hadoop Streaming | Big Data Hadoop Spark Tutorial...
Introduction to MapReduce - Hadoop Streaming | Big Data Hadoop Spark Tutorial...CloudxLab
 
Integrate Hive and R
Integrate Hive and RIntegrate Hive and R
Integrate Hive and RJunHo Cho
 
Scoop Job, import and export to RDBMS
Scoop Job, import and export to RDBMSScoop Job, import and export to RDBMS
Scoop Job, import and export to RDBMSRupak Roy
 
Introduction to scoop and its functions
Introduction to scoop and its functionsIntroduction to scoop and its functions
Introduction to scoop and its functionsRupak Roy
 
Stack Operations
Stack Operations Stack Operations
Stack Operations RidaZaman1
 
Cassandra Deep Diver & Data Modeling
Cassandra Deep Diver & Data ModelingCassandra Deep Diver & Data Modeling
Cassandra Deep Diver & Data ModelingBrian Enochson
 
Hadoop and HBase experiences in perf log project
Hadoop and HBase experiences in perf log projectHadoop and HBase experiences in perf log project
Hadoop and HBase experiences in perf log projectMao Geng
 
5 R Tutorial Data Visualization
5 R Tutorial Data Visualization5 R Tutorial Data Visualization
5 R Tutorial Data VisualizationSakthi Dasans
 
R data-import, data-export
R data-import, data-exportR data-import, data-export
R data-import, data-exportFAO
 
Introductive to Hive
Introductive to Hive Introductive to Hive
Introductive to Hive Rupak Roy
 
Import Database Data using RODBC in R Studio
Import Database Data using RODBC in R StudioImport Database Data using RODBC in R Studio
Import Database Data using RODBC in R StudioRupak Roy
 

What's hot (20)

MapReduce - Basics | Big Data Hadoop Spark Tutorial | CloudxLab
MapReduce - Basics | Big Data Hadoop Spark Tutorial | CloudxLabMapReduce - Basics | Big Data Hadoop Spark Tutorial | CloudxLab
MapReduce - Basics | Big Data Hadoop Spark Tutorial | CloudxLab
 
ACADILD:: HADOOP LESSON
ACADILD:: HADOOP LESSON ACADILD:: HADOOP LESSON
ACADILD:: HADOOP LESSON
 
Introduction to MapReduce - Hadoop Streaming | Big Data Hadoop Spark Tutorial...
Introduction to MapReduce - Hadoop Streaming | Big Data Hadoop Spark Tutorial...Introduction to MapReduce - Hadoop Streaming | Big Data Hadoop Spark Tutorial...
Introduction to MapReduce - Hadoop Streaming | Big Data Hadoop Spark Tutorial...
 
Integrate Hive and R
Integrate Hive and RIntegrate Hive and R
Integrate Hive and R
 
Unit 4-apache pig
Unit 4-apache pigUnit 4-apache pig
Unit 4-apache pig
 
Unit 4 lecture-3
Unit 4 lecture-3Unit 4 lecture-3
Unit 4 lecture-3
 
Scoop Job, import and export to RDBMS
Scoop Job, import and export to RDBMSScoop Job, import and export to RDBMS
Scoop Job, import and export to RDBMS
 
Introduction to scoop and its functions
Introduction to scoop and its functionsIntroduction to scoop and its functions
Introduction to scoop and its functions
 
Unit 2 part-2
Unit 2 part-2Unit 2 part-2
Unit 2 part-2
 
Algebra
AlgebraAlgebra
Algebra
 
Set, merge, and update
Set, merge, and updateSet, merge, and update
Set, merge, and update
 
Stack Operations
Stack Operations Stack Operations
Stack Operations
 
Cassandra Deep Diver & Data Modeling
Cassandra Deep Diver & Data ModelingCassandra Deep Diver & Data Modeling
Cassandra Deep Diver & Data Modeling
 
Hive practice
Hive practiceHive practice
Hive practice
 
Hadoop and HBase experiences in perf log project
Hadoop and HBase experiences in perf log projectHadoop and HBase experiences in perf log project
Hadoop and HBase experiences in perf log project
 
5 R Tutorial Data Visualization
5 R Tutorial Data Visualization5 R Tutorial Data Visualization
5 R Tutorial Data Visualization
 
R data-import, data-export
R data-import, data-exportR data-import, data-export
R data-import, data-export
 
Introductive to Hive
Introductive to Hive Introductive to Hive
Introductive to Hive
 
Laugh Your Hashes Off
Laugh Your Hashes OffLaugh Your Hashes Off
Laugh Your Hashes Off
 
Import Database Data using RODBC in R Studio
Import Database Data using RODBC in R StudioImport Database Data using RODBC in R Studio
Import Database Data using RODBC in R Studio
 

Similar to Draft sas and r and sas (may, 2018 asa meeting)

Similar to Draft sas and r and sas (may, 2018 asa meeting) (20)

Prog1 chap1 and chap 2
Prog1 chap1 and chap 2Prog1 chap1 and chap 2
Prog1 chap1 and chap 2
 
SAS_and_R.pdf
SAS_and_R.pdfSAS_and_R.pdf
SAS_and_R.pdf
 
Rmarkdown cheatsheet-2.0
Rmarkdown cheatsheet-2.0Rmarkdown cheatsheet-2.0
Rmarkdown cheatsheet-2.0
 
Sas Talk To R Users Group
Sas Talk To R Users GroupSas Talk To R Users Group
Sas Talk To R Users Group
 
Sas classes in mumbai
Sas classes in mumbaiSas classes in mumbai
Sas classes in mumbai
 
r,rstats,r language,r packages
r,rstats,r language,r packagesr,rstats,r language,r packages
r,rstats,r language,r packages
 
Basics Of SAS Programming Language
Basics Of SAS Programming LanguageBasics Of SAS Programming Language
Basics Of SAS Programming Language
 
Proc r
Proc rProc r
Proc r
 
Sas
SasSas
Sas
 
SAS - Training
SAS - Training SAS - Training
SAS - Training
 
Lecture1_R.pdf
Lecture1_R.pdfLecture1_R.pdf
Lecture1_R.pdf
 
SAS Internal Training
SAS Internal TrainingSAS Internal Training
SAS Internal Training
 
1 Installing & getting started with R
1 Installing & getting started with R1 Installing & getting started with R
1 Installing & getting started with R
 
Lecture1_R.ppt
Lecture1_R.pptLecture1_R.ppt
Lecture1_R.ppt
 
Lecture1_R.ppt
Lecture1_R.pptLecture1_R.ppt
Lecture1_R.ppt
 
Lecture1 r
Lecture1 rLecture1 r
Lecture1 r
 
Modeling in R Programming Language for Beginers.ppt
Modeling in R Programming Language for Beginers.pptModeling in R Programming Language for Beginers.ppt
Modeling in R Programming Language for Beginers.ppt
 
Amazon EMR Masterclass
Amazon EMR MasterclassAmazon EMR Masterclass
Amazon EMR Masterclass
 
Amazon EMR Masterclass
Amazon EMR MasterclassAmazon EMR Masterclass
Amazon EMR Masterclass
 
1 installing & Getting Started with R
1 installing & Getting Started with R1 installing & Getting Started with R
1 installing & Getting Started with R
 

More from Barry DeCicco

Easy HTML Tables in RStudio with Tabyl and kableExtra
Easy HTML Tables in RStudio with Tabyl and kableExtraEasy HTML Tables in RStudio with Tabyl and kableExtra
Easy HTML Tables in RStudio with Tabyl and kableExtraBarry DeCicco
 
Introduction to r studio on aws 2020 05_06
Introduction to r studio on aws 2020 05_06Introduction to r studio on aws 2020 05_06
Introduction to r studio on aws 2020 05_06Barry DeCicco
 
Beginning text analysis
Beginning text analysisBeginning text analysis
Beginning text analysisBarry DeCicco
 
Up and running with python
Up and running with pythonUp and running with python
Up and running with pythonBarry DeCicco
 
Using RStudio on AWS
Using RStudio on AWSUsing RStudio on AWS
Using RStudio on AWSBarry DeCicco
 
Calling python from r
Calling python from rCalling python from r
Calling python from rBarry DeCicco
 

More from Barry DeCicco (6)

Easy HTML Tables in RStudio with Tabyl and kableExtra
Easy HTML Tables in RStudio with Tabyl and kableExtraEasy HTML Tables in RStudio with Tabyl and kableExtra
Easy HTML Tables in RStudio with Tabyl and kableExtra
 
Introduction to r studio on aws 2020 05_06
Introduction to r studio on aws 2020 05_06Introduction to r studio on aws 2020 05_06
Introduction to r studio on aws 2020 05_06
 
Beginning text analysis
Beginning text analysisBeginning text analysis
Beginning text analysis
 
Up and running with python
Up and running with pythonUp and running with python
Up and running with python
 
Using RStudio on AWS
Using RStudio on AWSUsing RStudio on AWS
Using RStudio on AWS
 
Calling python from r
Calling python from rCalling python from r
Calling python from r
 

Recently uploaded

Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 

Recently uploaded (20)

Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 

Draft sas and r and sas (may, 2018 asa meeting)

  • 1. SAS AND R AND SAS Barry DeCicco Ann Arbor Chapter, American Statistical Association May 22, 2018
  • 2. CONTENTS Calling R from SAS. Calling SAS from R. References.
  • 3. CALLING R FROM SAS USING SAS/IML
  • 4. WHY USE R IF YOU HAVE SAS? You can use SAS for what you already know well, and use R only as needed. You might find that you don’t have the capacities in SAS which you desire. R has vast and rapidly growing capacities – over 10,000 packages.
  • 5. BASICS OF R R must be installed on your computer. Download from CRAN: https://cran.r-project.org Necessary packages (modules) must be downloaded from CRAN and installed: (select ‘Packages’ from the left- hand menu). You will need to set up SAS to use R.
  • 6. SETTING UP SAS TO USE R You need to edit the file ‘SASv9.cfg’, which controls some configurations (run ‘proc options option=config;’ to find it). Make a copy, and then edit the original, adding these lines: 1. -RLANG 2. -config "C:Program FilesSASHomeSASFoundation9.4nlsensasv9.cfg“[replace with folder name for where this file is on your computer] 3. -SET R_HOME "C:Program FilesRR-3.5.0“ [replace with folder name for where R lives on your computer]
  • 7. HOW TO SUBMIT R CODE IN SAS When running analyses/data manipulations in R, there are things which need to be done: Setting directories as needed, to pull/save data, output and graph files. Loading (already installed!) necessary packages for use (e.g., ‘ggplot2’ for complex graphs). These will need to be done in the SAS code submitted to R each time.
  • 8. STARTING LINES Use PROC Options to enable SAS to use R: proc options option=rlang; run; Start IML (this has to be done through SAS/IML): Proc IML; Export SAS datasets to R as needed: Call ExportDataSetToR(SAS-data-set, RDataFrame); Let SAS know to start R: submit / R;
  • 9. ENDING LINES Retrieve data sets from R as needed:  CALL IMPORTDATASETFROMR (SAS-data-set, RExpr) ; Let SAS know to end sending commands to R: endsubmit; Quit IML (if no further IML use is needed): quit;
  • 10. R LANGUAGE COMMANDS Set the working directory (location where R retrieves and stores files), while storing the previous location in ‘old_wd’:  old_wd<-setwd("C:/Users/Barry/Desktop") Resetting the working directory back to what it was: setwd(old_wd); # this is a comment
  • 11. GOING THROUGH THE LOG - PRELIMINARIES  /* Start R options/ */  proc options option=rlang;  run;  proc iml;  title "Statistic in R (integration with SAS)";  run ExportDataSetToR("WORK.RCBD_TEST", "RCBD_IN_R");  submit / R;
  • 12. GOING THROUGH THE LOG – SUBMITTING COMMANDS TO R  #_______Beginning of R code_________________  # Change the working directory, saving the old one in ‘old_wd’  old_wd<-setwd("C:/Users/Barry/Desktop")  # Load the ‘qicharts’ module (package), so that it can be used.  library(qicharts)
  • 13. GOING THROUGH THE LOG – SUBMITTING COMMANDS TO R  # Create a data frame (data set)  d2 <- data.frame(y = rnorm(144, 12, 3),  expand.grid(x = seq.Date(as.Date('2014-1-1'),  by = 'week',  length.out = 24),  g1 = LETTERS[1:3],  g2 = letters[1:2]))  # Introduce a shift in process performance  d2$y[132:144] <- d2$y[132:144] * 3
  • 14. GOING THROUGH THE LOG – SUBMITTING COMMANDS TO R  # Run a chart.  trc(y ~ x | g1 + g2, data = d2, main = 'Trellis run chart', chart = 'i')  # Direct the graphics output to a file, then close it.  dev.copy(pdf,'myplot.pdf',width=6,height=4 )  dev.off()  # Reset the working directory  setwd(old_wd)
  • 15. GOING THROUGH THE LOG – WRAPPING UP  #_______End of R code_________________  endsubmit; # Any error messages from R will now show up  * Bring the data set from R to SAS;  CALL IMPORTDATASETFROMR ("work.d2", "d2") ;  proc print data=d2;  run;
  • 17. CALLING SAS FROM R USING KNITR AND MARKDOWN
  • 18. WHAT IS KNITR? knitr is an R package which combines code, comments, text and results into one document. You write a document with text and code chunks. knitr will run the code chunks and incorporate the results, knitting them into a single document. This document can be in Word, HTML or PDF (the latter requires outside software).
  • 19. STARTING OFF – IN RSTUDIO
  • 20. THREE OUTPUT OPTIONS HTML – easiest to start with. Word. PDF – this requires external software to finish. I will use HTML
  • 21. STARTING THROUGH THE DOCUMENT Knitr will set up a standard starting document prepopulated with some items. Title information. Initial setup. Explantion
  • 22. CODE CHUNKS AND SETTING UP SAS  Gray items are ‘code chunks’, where there is code to be executed.  They start and end with ```  This sets up SAS in this code chunk. It tells R to use SAS, and where SAS is.
  • 23. LIMITATIONS Each code chunk runs a separate batch job in SAS. There is no hand-off of WORK. data sets between SAS code chunks. To hand off data sets (between SAS and SAS, or SAS and R), save them as permanent files. SAS HTML output plays well with knitr’s HTML markdown. For other types of output, other packages are needed. Slashes/back-slashes in path names need to be managed.
  • 24. SET UP Let R know that it’s using sas (the ‘SAS’ or ‘SASHTML’ engine). Tell R where the SAS.exe file is. Set any SAS options. These can by done either by chunk, or overall (for now, overall).
  • 25. SET UP There are at least 20 ‘engines’ available for knitr, meaning 20 languages. For SAS, there is the ‘sas’ and ‘sashtml’ engines. For this presentation, the ‘sashtml’ engine will be used.
  • 26. SET UP Once the set up has been done overall, then SAS commands can be done.
  • 28. REFERENCES – SAS  SAS/IML(R) 9.3 User's Guide ‘The RLANG System Option’ http://support.sas.com/documentation/cdl/en/imlug/64248/HTM L/default/viewer.htm#imlug_r_sect003.htm  SAS/IML(R) 9.3 User's Guide‘Submit R Statements’ http://support.sas.com/documentation/cdl/en/imlug/64248/HTM L/default/viewer.htm#imlug_r_sect004.htm  SAS/IML(R) 12.1 User's Guide: EXPORTDATASETTOR Call http://support.sas.com/documentation/cdl/en/imlug/65547/HTM L/default/viewer.htm#imlug_langref_sect114.htm  SAS/IML(R) 12.1 User's Guide: IMPORTDATASETFROMR Call http://support.sas.com/documentation/cdl/en/imlug/65547/HTM L/default/viewer.htm#imlug_langref_sect182.htm
  • 29. REFERENCES – SAS (CON.)  ‘Run R code inside SAS easily’ SAS/IML(R) 9.3 User's Guide ‘EXPORTDATASETTOR Call’ http://support.sas.com/documentation/cdl/en/imlug/65547/HTML/default /viewer.htm#imlug_langref_sect114.htm  SAS Blogs: Video: Calling R from the SAS/IML Language https://blogs.sas.com/content/iml/2011/10/31/video-calling-r-from-the- sasiml-language.html
  • 30. REFERENCES - R  ‘SAS Using R Markdown (Windows)’ https://www.ssc.wisc.edu/~hemken/SASworkshops/M arkdown/SASmarkdown.html#html-or-pdf-document- as-a-final-target  The raw markdown version of the above page: https://www.ssc.wisc.edu/~hemken/SASworkshops/M arkdown/SASmarkdown.rmd  ‘Tables and Graphs - Using the sashtml Engine’ https://www.ssc.wisc.edu/~hemken/SASworkshops/M arkdown/SAShtmlengine.html  Dynamic Documents with R and Knitr (by Yihui Xie) https://yihui.name/knitr/