BookmarkSubscribeRSS Feed
DanielRingqvist
SAS Employee

Did you know Base SAS can execute Python code? Starting with May 2019 release of SAS 9.4M6, the PROC FCMP procedure added support for submitting and executing functions written in Python from within a SAS session using the new Python object. Proc FCMP is where you create your own SAS functions, and this alone is a brilliant way of cleaning up your code. Now with 9.4M6 you can also execute python functions.

 

It could look like this simple example below, and there are many good sources to study such as blogs and SAS Documentation (links below snippet).

The necessary steps are these two and are performed where your SAS Foundation is installed:

  1. Install Python
  2. Add two environment variables in Windows (read doc to learn steps needed for other OS such as linux)
  3. Start SAS and submit this code

Many more examples in SAS Documentation and blog entries. 

 

Example code:

/*  ENVIRONMENT VARIABLES 
  - MAS_M2PATH: C:\SAS\SASHome\SASFoundation\9.4\tkmas\sasmisc\mas2py.py 
  - MAS_PYPATH: C:\Python27\python.exe
*/
proc fcmp outlib=work.fcmp.pyfuncs;
function MyPyFunc(FCMParg);
declare object py(python);
submit into py;
def TimesFive(PythonArg):
    "Output: MyKey"
    newvar = PythonArg * 5
    return newvar
endsubmit;
rc = py.publish();
rc = py.call("TimesFive",FCMParg);
MyFCMPResult = py.results["MyKey"];
return(MyFCMPResult);
endsub;
run;

options cmplib=work.fcmp;
data _null_;
   x = MyPyFunc(5);
   put x=;
run;

Recommended reading:

 

SAS or Python? Why not use both? Using Python functions inside SAS programs

https://blogs.sas.com/content/sgf/2019/06/04/using-python-functions-inside-sas-programs/

 

Using PROC FCMP Python Objects

https://go.documentation.sas.com/?docsetId=lecompobjref&docsetTarget=p18qp136f91aaqn1h54v3b6pkant.ht...