Run R code inside SAS easily

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 8
Accepted Solution

Run R code inside SAS easily

[ Edited ]

*RUNNING R LANGUAGE INSIDE SAS ENVIRONMENT;

 

*by: (Alysson Jalles);

 

*First of all you have to set your SAS software to run R codes:

  .You must have the SAS 9.22 or above.

  . XCMD option must be enabled (sometimes not the case in a client-server setup like SAS Enterprise Guide or SAS Studio)

  .Each version of SAS is able to run a specific R version, check out the link below to know what's the best option for you.

 

http://blogs.sas.com/content/iml/files/2013/09/Rversions.png_

  (source: http://blogs.sas.com/content/iml/2013/09/16/what-versions-of-r-are-supported-by-sas.html);

 

R user who is learning SAS programming? Check out this free course and materials

 

/*Setting SAS to RUN R comands - BEGIN******************************:

_____________________________________________________________________

1) Locate and open in Notepad or Notepad++ the file "sasv9.cfg".

In SAS 9.4 in windows OS the file is allocated in:

 

C:\Program Files\SASHome\SASFoundation\9.4\sasv9.cfg

 

.If possible make a backup of this file "sasv9.cfg".

 

2) Add this 3 lines of command in the end "sasv9.cfg" file, after that save,

   .You have to change your "sasv9.cfg" location, according your SAS version(if necessary)

   .You have to change your R location in "R_HOME", according your R version (if necessary):

 

-RLANG

-config "C:\Program Files\SASHome\SASFoundation\9.4\nls\en\sasv9.cfg"

-SET R_HOME "C:\Program Files\R\R-3.1.3"

 

3) Open your SAS foundation and Run in SAS editor:

 

proc options option=rlang;

run;

 

.In your SAS log must appear a message like this:

SAS (r) Proprietary Software Release 9.4  TS1M2

RLANG             Enables SAS to execute R language statements.

NOTE: PROCEDURE OPTIONS used (Total process time):

      real time           0.01 seconds

      cpu time            0.01 seconds

.Congratulations, now you can run R inside SAS;

____________________________________________________________________________________

/*Setting SAS to RUN R comands - END*******************************************************************:*/

 

*=======================================================================;

*Simulating a RCBD experiment ===========================================BEGIN;

*=======================================================================;

*10 TREATMENTS;

*3 REPLICATION;

proc plan seed=123456;

factors GEN=10 ordered REP=3 ordered/noprint;

output out=RCBD_TEST

GEN cvals=('GEN1' 'GEN2' 'GEN3' 'GEN4' 'GEN5' 'GEN6' 'GEN7' 'GEN8' 'GEN9' 'GEN10')

REP nvals=(1 2 3);

run;

 

*Simulating VARIABLES - BEGIN;

data RCBD_TEST; set RCBD_TEST;

  call streaminit(8789); /*SEED= 8789*/

VAR1=rand('normal',110, 15); /*MEAN 110: STD: 15*/

run;

data RCBD_TEST; set RCBD_TEST;

  call streaminit(907854); /*SEED= 907854*/

VAR2=rand('normal',3000, 751);run; /*MEAN 3000: STD: 751*/

*Simulating VARIABLES - END;

*=======================================================================;

*Simulating and RCBD experiment =====================================END;

*=======================================================================;

 

*Printing dataset, first 6 observations of simulated dataset:;

proc print data=RCBD_TEST (obs=6); run;

 

/*Testing if R codes is working*/

/*Histogram and ANOVA of VAR1 in R language*/

proc iml;

title "Statistic in R (integration with SAS)";

run ExportDataSetToR("WORK.RCBD_TEST", "RCBD_IN_R");

submit / R;

#_______Begin of R code_________________

setwd("C:/")

#1)Histogram in R

  #HISTOGRAM

  HIST_R_GRAPH<-hist(RCBD_IN_R$VAR1

  ,col="azure3",                        #color

  main="Histogram Of  - VAR1", #title

  ylab="frequency",                   #Y label

  xlab="Class")                         #X label

 

  #Exporting histogram graph to C:/ of Windows environment

       #plot(HIST_R_GRAPH)

  dev.copy(png, filename="Histogram_output.png", width=5.25,height=5.25,units="in",  res=200)

       #plot(HIST_R_GRAPH)dev.off()

 

#2) ANOVA in R [All sources of variation as fixed]

RESULT_ANOVA<-aov(VAR1~REP+GEN, data=RCBD_IN_R);

summary(RESULT_ANOVA);

 

#_______End of R code_________________

endsubmit;

*quit; /*Use Quit command to close GRAPHS*/


Accepted Solutions
Solution
‎07-18-2017 09:05 AM
Community Manager
Posts: 2,761

Re: Run R code inside SAS easily

[ Edited ]

Thanks for sharing these @invisibleman.  For more about SAS and R integration, see:

And another point from Rick:

  • The SAS/IML documentation contains a chapter "Calling Functions in the R Language" which provides an overview of how to set up SAS, call R, and transfer data back and forth. The documentation provides several examples.

View solution in original post


All Replies
Solution
‎07-18-2017 09:05 AM
Community Manager
Posts: 2,761

Re: Run R code inside SAS easily

[ Edited ]

Thanks for sharing these @invisibleman.  For more about SAS and R integration, see:

And another point from Rick:

  • The SAS/IML documentation contains a chapter "Calling Functions in the R Language" which provides an overview of how to set up SAS, call R, and transfer data back and forth. The documentation provides several examples.
SAS Super FREQ
Posts: 3,478

Re: Run R code inside SAS easily

I will add another bullet to Chris's list:

  • The SAS/IML documentation contains a chapter "Calling Functions in the R Language" which provides an overview of how to set up SAS, call R, and transfer data back and forth. The documentation provides several examples. 
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 9840 views
  • 2 likes
  • 3 in conversation