0

I have an .R file that has a few functions stored in it. Functions as in FunctionName <- function(x). I am trying to write a SAS code that references this R file and can call and run one of the functions in the R file. Here is my code:

proc iml;
submit /R;
source(file = "C:/blah/My Documents/R/TheRFile.R")
function1("2014-05-25")
endsubmit;
quit;

And here is one of the functions in my R file:

function1 <- function(x){

refdate <- as.Date(x)

#MORE STUFF HERE
}

Essentially, the R functions in this R file take in a date as the parameter, and it uses this parameter to decide how to sort certain data files. When I run the SAS code I get:

NOTE: IML Ready
3393  submit /R;
3394  source(file = "C:/blah/My Documents/R/TheRFile.R")
3395  function1("2014-05-25")
3396  endsubmit;
ERROR: R: Error: could not find function "function1"

statement : SUBMIT at line 3393 column 1
3397  quit;
NOTE: Exiting IML.
NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE IML used (Total process time):
real time           0.71 seconds
cpu time            0.01 seconds

I tried using the SAS MACRO command but that just got me even more confused. I think I explained what I want to do clearly. I understand the submit /R lets you run R code within that block of SAS code, but I am actually trying to access the functions of an external .R file. I've been pulling my hair out googling and there seems to be nothing on what I am trying to do. I have changed the names of the functions/variables/file locations but it should still make sense. Thanks.

SoloSpirit
  • 87
  • 12
  • 1
    After do you the `source()` if you run an `ls()` instead, does the function name show up? – MrFlick Jun 28 '17 at 15:14
  • This was helpful. My R code has 4 functions and running ls() in SAS only shows it recognizing 2 of the functions. Weird, all 4 functions run in R - I tested it with one of the ones it recognizes and it appears to be running. Wonder why it doesn't recognize the others. – SoloSpirit Jun 28 '17 at 15:19
  • 2
    It's hard to say because I can't replicate the behavior with the example you provided. That works just fine for me. It would be better if you provided a [mcve] – MrFlick Jun 28 '17 at 15:27
  • Write a new file with just the simple function in it, nothing else, and simple SAS program just as above, and see if it works or has this issue. I had no issue either replicating the above. – Joe Jun 28 '17 at 15:29
  • 2
    I would check that you have the path to the file right (that you're not running, say, an old copy). – Joe Jun 28 '17 at 15:35

0 Answers0