Why can't I embed PROC R in a macro?

Nico Chart_21517
Nico Chart_21517
Altair Employee
edited April 2023 in Altair RapidMiner

The SAS language and the R language have very different syntax and semantics so it is not possible to place R procedure calls inside SAS language macro procedures. This may appear to be a limitation but the two languages have different interpretations of special characters and keywords that would cause conflicts with SAS language macro parsing.

For more sophisticated applications you may well wish to use the SAS language to generate a parameterized set of R language statements before executing them. This may involve an external file for example:

data _null_;
  file "Rpgm1.r" recfm=v;
  put "print(source)";
run;

proc r;
  export data=dataset R=source;
  submit;
  source("Rpgm1.r", echo=T)
  endsubmit;
run;

or alternatively using a SAS language macro:

%macro myRmacro(DSN);
data _null_;
  file "Spgm1.sas" recfm=v;
  put 'PROC R;';
  put "EXPORT data=" &DSN " R=source;";
  put 'SUBMIT;';
  put 'print(source)';
  put 'ENDSUBMIT;';
  put 'run;';
run;
%include "Spgm1.sas" / source2;
%mend myRmacro;
%myRmacro("dataset");

 

Tagged: