SlideShare a Scribd company logo
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

PPTX
Calling r from sas (msug meeting, feb 17, 2018) revised
PPT
Arrays in SAS
PDF
Apache Scoop - Import with Append mode and Last Modified mode
PDF
Map Reduce Execution Architecture
PDF
Import web resources using R Studio
PPTX
PDF
Import and Export Big Data using R Studio
PDF
Apache Hive Table Partition and HQL
Calling r from sas (msug meeting, feb 17, 2018) revised
Arrays in SAS
Apache Scoop - Import with Append mode and Last Modified mode
Map Reduce Execution Architecture
Import web resources using R Studio
Import and Export Big Data using R Studio
Apache Hive Table Partition and HQL

What's hot (20)

PDF
MapReduce - Basics | Big Data Hadoop Spark Tutorial | CloudxLab
ODT
ACADILD:: HADOOP LESSON
PDF
Introduction to MapReduce - Hadoop Streaming | Big Data Hadoop Spark Tutorial...
PDF
Integrate Hive and R
PPTX
Unit 4-apache pig
PPTX
Unit 4 lecture-3
PDF
Scoop Job, import and export to RDBMS
PDF
Introduction to scoop and its functions
PPTX
Unit 2 part-2
PPT
Algebra
PDF
Set, merge, and update
PPTX
Stack Operations
PPTX
Cassandra Deep Diver & Data Modeling
PDF
Hive practice
PPTX
Hadoop and HBase experiences in perf log project
PDF
5 R Tutorial Data Visualization
PDF
R data-import, data-export
 
PDF
Introductive to Hive
DOCX
Laugh Your Hashes Off
PDF
Import Database Data using RODBC in R Studio
MapReduce - Basics | Big Data Hadoop Spark Tutorial | CloudxLab
ACADILD:: HADOOP LESSON
Introduction to MapReduce - Hadoop Streaming | Big Data Hadoop Spark Tutorial...
Integrate Hive and R
Unit 4-apache pig
Unit 4 lecture-3
Scoop Job, import and export to RDBMS
Introduction to scoop and its functions
Unit 2 part-2
Algebra
Set, merge, and update
Stack Operations
Cassandra Deep Diver & Data Modeling
Hive practice
Hadoop and HBase experiences in perf log project
5 R Tutorial Data Visualization
R data-import, data-export
 
Introductive to Hive
Laugh Your Hashes Off
Import Database Data using RODBC in R Studio
Ad

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

PPT
Prog1 chap1 and chap 2
PDF
SAS_and_R.pdf
PDF
Rmarkdown cheatsheet-2.0
PDF
Sas Talk To R Users Group
PPT
Sas classes in mumbai
PPT
r,rstats,r language,r packages
PPT
Basics Of SAS Programming Language
PDF
Proc r
PPT
Lecture1_R Programming Introduction1.ppt
PPT
R_Language_study_forstudents_R_Material.ppt
PPT
Brief introduction to R Lecturenotes1_R .ppt
PDF
Basics of R programming for analytics [Autosaved] (1).pdf
PDF
Lecture1_R.pdf
PDF
SAS Internal Training
PDF
1 Installing & getting started with R
PPT
Lecture1_R.ppt
PPT
Lecture1 r
PPT
Modeling in R Programming Language for Beginers.ppt
Prog1 chap1 and chap 2
SAS_and_R.pdf
Rmarkdown cheatsheet-2.0
Sas Talk To R Users Group
Sas classes in mumbai
r,rstats,r language,r packages
Basics Of SAS Programming Language
Proc r
Lecture1_R Programming Introduction1.ppt
R_Language_study_forstudents_R_Material.ppt
Brief introduction to R Lecturenotes1_R .ppt
Basics of R programming for analytics [Autosaved] (1).pdf
Lecture1_R.pdf
SAS Internal Training
1 Installing & getting started with R
Lecture1_R.ppt
Lecture1 r
Modeling in R Programming Language for Beginers.ppt
Ad

More from Barry DeCicco (6)

PDF
Easy HTML Tables in RStudio with Tabyl and kableExtra
PDF
Introduction to r studio on aws 2020 05_06
PDF
Beginning text analysis
PDF
Up and running with python
PPTX
Using RStudio on AWS
PPTX
Calling python from r
Easy HTML Tables in RStudio with Tabyl and kableExtra
Introduction to r studio on aws 2020 05_06
Beginning text analysis
Up and running with python
Using RStudio on AWS
Calling python from r

Recently uploaded (20)

PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPT
Introduction Database Management System for Course Database
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PPTX
L1 - Introduction to python Backend.pptx
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PPTX
ISO 45001 Occupational Health and Safety Management System
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
VVF-Customer-Presentation2025-Ver1.9.pptx
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Introduction Database Management System for Course Database
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Wondershare Filmora 15 Crack With Activation Key [2025
Operating system designcfffgfgggggggvggggggggg
Design an Analysis of Algorithms II-SECS-1021-03
How Creative Agencies Leverage Project Management Software.pdf
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
L1 - Introduction to python Backend.pptx
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
ISO 45001 Occupational Health and Safety Management System
ManageIQ - Sprint 268 Review - Slide Deck
2025 Textile ERP Trends: SAP, Odoo & Oracle

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/