BookmarkSubscribeRSS Feed
nstdt
Quartz | Level 8

I need to use Python within SAS 9.4.

I have Anaconda installed on my laptop. The IT in my organisation was unable to help me. 

What is the process to start using Python from SAS 9.4.?

 

Could anyone advice me on what additional SAS modules or extensions I need?

 

Thank you

18 REPLIES 18
Quentin
Super User

There are some tips in this thread:

https://communities.sas.com/t5/SAS-Programming/Executing-python-code-from-SAS-Environment/td-p/76964...

BASUG is hosting free webinars Next up: Mark Keintz presenting History Carried Forward, Future Carried Back: Mixing Time Series of Differing Frequencies on May 8. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
Patrick
Opal | Level 21

Python must be installed on a machine where SAS can call it. That's not the machine where your SAS client runs but where the SAS Server executes the SAS code.

I've done something like this a few months back in a SAS9.4 environment. Python was already installed on the server but I had to request additional libraries. Anaconda is certainly not installed and something you would need to request from your IT department/SAS Admins.

 

In my case integration between SAS and Python was very simple: SAS did the data prep and created a SAS table, then I just called the Python program via call system() that then read the SAS table via Pandas and did its thing (in my case created an Excel workbook with functionality like drop down validation that I couldn't do with SAS).

nstdt
Quartz | Level 8

Thanks for the reply. When you say "That's not the machine where your SAS client runs but where the SAS Server executes the SAS code" - I assume you mean that Python needs to be installed in the Server , not just my laptop (I am not even very sure what a server is, but I do know it's not my local laptop). I guess I must call the IT desk and ask them if they have Python in the server used by my department. If I'm missing anything, please let me know.

Also, as the other response mentioned, I tried following the suggestions here:https://go.documentation.sas.com/doc/en/bicdc/9.4/biasag/n1mquxnfmfu83en1if8icqmx8cdf.htm

 

and typed in the commands given there  into SAS but only get these Error messages in the Log:

 

1    set MAS_M2PATH="C:\SASServer\SASHome\SASFoundation\9.4\tkmas\sasmisc\mas2py.py"
     ---
     180
ERROR 180-322: Statement is not valid or it is used out of proper order.

2    set MAS_PYPATH="c:\python\python.exe"

 

Patrick
Opal | Level 21

Yes, not your laptop unless you've got a local SAS installation which becomes rarer and rarer.

 

You can get the name of the server where SAS executes by issuing below in a SAS command Window

%put &=syshostname;

The set commands are OS commands that create and populate OS level environment variables. 

You can also create and populate such variables out of a SAS session using EXAMPLE code like below. ...don't execute it. The path must match your environment, and it's best done by an Admin as part of environment configuration.

options SET=MAS_PYPATH="c:\python\python.exe";
nstdt
Quartz | Level 8

Hmm, in my case, I may actually have a local SAs installation - but I really don't know for sure.

I tried your put code - does this mean anything to you? Thanks again!

 

1    %put &=syshostname;
SYSHOSTNAME=DESKTOP-JU738BM


 

Patrick
Opal | Level 21

This could indicate that you are running SAS locally which then also would mean you can call your local Python.

What client are you using for running SAS? EG, Studio, "PC SAS"? 

If using EG is the server you're connecting to called "local"? 

nstdt
Quartz | Level 8

It's not EG for sure. I think it's PC SAS. I just pull it up from my Start menu on Windows , on my personal laptop. There's no official password or anything, although it was installed through my work department.

nstdt
Quartz | Level 8

I can see this as well:

nstdt_0-1676774813818.png

 

Patrick
Opal | Level 21

Then you're at luck. You're using the old fashioned display manager that sits right on top of SAS. 

It depends a bit what you want to do with Python but if it's something as simple as what I did then it's just about calling Python out of SAS via a call system() or X command or similar.

SASKiwi
PROC Star

Does this post help - the code examples should work equally as well for you.

Sajid01
Meteorite | Level 14

Hello @nstdt 
The SAS documentation requires creating environment variables at the operating system level rather then run the set command in the SAS EG as you have shown in the error message.
In case you are using Windows, You can either run from the command line or set the environment variable in the GUI. For this open up all settings and in the window that opens type Environment variables. There are a number of tutorials on google for this.

nstdt
Quartz | Level 8

Dear all,

Since I have SAS9.4 installed on my local Windows machine, I tried setting the environment variables  as follows:

MAS_PYPATH = C:\Users\radhi\AppData\Local\Programs\Python\Python310\python.exe

 

and 

 

MAS_M2PATH = C:\Program Files\SASHome\SASFoundation\9.4\tkmas\sasmisc\mas2py.py

 

And then I tried running the SAS code from their help pages to check:

proc fcmp;
declare object py(python);
submit into py;
def PyProduct(var1, var2):
    "Output: MyKey"
    newvar = var1 * var2
    return newvar,
endsubmit;
rc = py.publish();
rc = py.call("PyProduct", 5, 10);
MyResult = py.results["MyKey"];
put MyResult=;
run;

However, the SAS log shows the following errors:

ERROR: Module 'py' failed to compile in user context 'CMP User Thread'.
NOTE: Module 'py' compilation errors: 
ERROR: MAS_PYPATH environment variable is undefined.
ERROR:
      tkmaspyPublish encountered a failure in tkmaspyConnect, rc=0x803FC26A.
ERROR: masPublish encountered a failure.
ERROR: Foreign Language Compilation messages:
ERROR: Python compile: ERROR: MAS_PYPATH environment variable is undefined.
ERROR: Python compile: ERROR: tkmaspyPublish encountered a failure in tkmaspyConnect,
       rc=0x803FC26A.
ERROR: Unable to publish program for foreign language execution.
ERROR: Error reported in function 'python:PUBLISH' in statement number 4 at line 80 column 1.
       The statement was:
    1     (80:1)     rc = python:PUBLISH(  )
NOTE: PROCEDURE FCMP used (Total process time):
      real time           0.14 seconds
      cpu time            0.06 seconds

Any idea what may be going wrong?

 

Thanks.

 

 

nstdt
Quartz | Level 8

 edited the PYPATH as follows:

C:\Users\radhi\anaconda3

but still get errors as below:

ERROR: Module 'py' failed to compile in user context 'CMP User Thread'.
NOTE: Module 'py' compilation errors: 
ERROR: MAS_PYPATH environment variable is undefined.
ERROR:
      tkmaspyPublish encountered a failure in tkmaspyConnect, rc=0x803FC26A.
ERROR: masPublish encountered a failure.
ERROR: Foreign Language Compilation messages:
ERROR: Python compile: ERROR: MAS_PYPATH environment variable is undefined.
ERROR: Python compile: ERROR: tkmaspyPublish encountered a failure in tkmaspyConnect,
       rc=0x803FC26A.
ERROR: Unable to publish program for foreign language execution.
ERROR: Error reported in function 'python:PUBLISH' in statement number 4 at line 94 column 1.
       The statement was:
    1     (94:1)     rc = python:PUBLISH(  )
NOTE: PROCEDURE FCMP used (Total process time):
      real time           0.13 seconds
      cpu time            0.09 seconds

Sajid01
Meteorite | Level 14

Sorry to hear about the error.

The error "ERROR: MAS_PYPATH environment variable is undefined." implies
that this environment variable is not available.
The environment variable must be defined either as a system environment
variable OR as the user running the SAS code.
Defining system environment variable requires administrator privileges.
On a Desktop this must be done as an administrator user.

The value of environment variable *MAS_PYPATH* should be the full path
to the location where python interpreter "python.exe" is actually
installed.Please add this location of python interpreter to the PATH
environment variable too.

suga badge.PNGThe SAS Users Group for Administrators (SUGA) is open to all SAS administrators and architects who install, update, manage or maintain a SAS deployment. 

Join SUGA 

Get Started with SAS Information Catalog in SAS Viya

SAS technical trainer Erin Winters shows you how to explore assets, create new data discovery agents, schedule data discovery agents, and much more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 18 replies
  • 1866 views
  • 6 likes
  • 7 in conversation