DATA Step, Macro, Functions and more

Run R code inside SAS easily

New Contributor
Posts: 3

Run R code inside SAS easily

Thank you ALL for your feedback.


Another try --- I am trying to run the following code (from Listen Data site).  


Both SAS and R are on server and windows verstion (instead of SAS UE on server and R on local machine).


setwd() below does not seem to work, nor running R code from SAS.  Any suggestions!!


Please see rpath in (attached) -- which came from a JSS paper. Inside Proc_R, as suggested, I used rpath=%str(C:\Progra~1\R\R-3.3.2\bin\R.exe


%include "C:\test\"; 

data example;
input Srl x1-x5;
1 . 89 85 . 87
2 79 73 74 . .
3 80 82 . 85 .

************* EXAMPLE 1 *************;
/*Run Summary Statistics*/
%Proc_R(SAS2R=example, R2SAS=);


proc print data=rlog;


Super User
Posts: 13,583

Re: Run R code inside SAS easily

When you say "below does not seem to work" please describe how it doesn't work. Do you get errors or unexpected warning? Post the code with the log that shows the messages. If inside a macro, run the code with OPTIONS MPRINT; turned on. Post the result in a code box using the {i} icon to keep the forum from re-formatting the text.


If you get unexpected output describe the expected result and show the actual result.



Did you open the generated text file with what I think is supposed to be R code and verify the code written there was as intended?

New Contributor
Posts: 3

Re: Run R code inside SAS easily

Sorry for the confusion, should have added more details. I used MPRINT, and the text files (which you asked) were not found in the work directory assigned by setwd() statement.


The code basically hangs and only way out is Cancel Submitted Statements.  I attached the code with few %put inserted.  


Since the previous attempt of attaching the log file did not go through for some reason, below is the log.  Many thanks,


251 %include "C:\EA_test\";
502 data example;
503 input Srl x1-x5;
504 cards;

NOTE: The data set WORK.EXAMPLE has 3 observations and 6 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

508 ;
509 run;
510 %put "HI 001";
"HI 001"
512 /*Run Summary Statistics*/
513 %Proc_R(SAS2R=example,R2SAS=);
MPRINT(PROC_R): options notes;
MPRINT(PROC_R): data _null_;
MPRINT(PROC_R): call symput('r_code','r_code'||trim(left(scan(put(datetime(),best.),1,'.'))));

NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

MPRINT(PROC_R): data _null_;
MPRINT(PROC_R): file "C:\Users\TEF419~1\AppData\Local\Temp\SAS Temporary
MPRINT(PROC_R): infile cards4;
MPRINT(PROC_R): input;
MPRINT(PROC_R): put _infile_;
514 cards4;

NOTE: The file "C:\Users\TEF419~1\AppData\Local\Temp\SAS Temporary
Files\_TD2256_WINDOWS7STATIC2_\r_code1802690920.r" is:

Filename=C:\Users\TEF419~1\AppData\Local\Temp\SAS Temporary
RECFM=V,LRECL=32767,File Size (bytes)=0,
Last Modified=14Feb2017:11:28:40,
Create Time=14Feb2017:11:28:40

NOTE: The DATA step has been abnormally terminated.



Ask a Question
Discussion stats
  • 2 replies
  • 2 in conversation