DATA Step, Macro, Functions and more

Connect to SAS using Python saspy

Reply
Contributor
Posts: 60

Connect to SAS using Python saspy

I  have SAS base local on Windows machine and i want to use python to pull a dataset from a sas library to do a decision tree, I installed saspy successfully but when i try to open a sas session using 

 

sas = saspy.SASsession( cfgname='winlocal')

 

it just runs for a long time no errors but doesnt connect.

 

Any help would be valuable.

 

Thanks,

Myrto

 

Super User
Posts: 19,860

Re: Connect to SAS using Python saspy

Are you planning to do the decision tree in SAS or Python?

A SAS library is just a folder with files, possibly on a server. If you're doing the decision tree in python it would make more sense to bypass SAS entirely, wouldn't it?

Community Manager
Posts: 2,955

Re: Connect to SAS using Python saspy

Make sure that your sascfg.py points to the proper location for you local install and Java runtime.  Ex:

 

winlocal = {'java'      : 'C:\\Program Files\\SASHome\\SASPrivateJavaRuntimeEnvironment\\9.4\\jre\\bin\\java',
            'encoding'  : 'windows-1252',
            'classpath' : cpW
            }

To find your sascfg.py, use these console commands:

 

>>> import saspy
>>> saspy.SAScfg
<module 'saspy.sascfg' from 'C:\\ProgramData\\Anaconda3\\lib\\site-packages\\saspy\\sascfg.py'>
>>> 
Contributor
Posts: 60

Re: Connect to SAS using Python saspy

Posted in reply to ChrisHemedinger

now i get this error:

 

sas = saspy.SASsession(cfgname='default')
Cannot use STDIO I/O module on Windows. No SASsession established. Choose an IOM SASconfig definition

 

Thanks

Community Manager
Posts: 2,955

Re: Connect to SAS using Python saspy

"default" does not work on Windows.  You need to use winlocal or winiomwin.  You might need to edit the sascfg.py file I mentioned to enable winlocal.  

 

Mine looks like this:

# build out a local classpath variable to use below for Windows clients
cpW  =  "C:\Program Files\SASHome\SASDeploymentManager\9.4\products\deploywiz__94470__prt__xx__sp0__1\deploywiz\sas.svc.connection.jar"
cpW += ";C:\Program Files\SASHome\SASDeploymentManager\9.4\products\deploywiz__94470__prt__xx__sp0__1\deploywiz\log4j.jar"
cpW += ";C:\Program Files\SASHome\SASDeploymentManager\9.4\products\deploywiz__94470__prt__xx__sp0__1\deploywiz\sas.security.sspi.jar"
cpW += ";C:\Program Files\SASHome\SASDeploymentManager\9.4\products\deploywiz__94470__prt__xx__sp0__1\deploywiz\sas.core.jar"
cpW += ";C:\ProgramData\Anaconda3\Lib\site-packages\saspy\java\saspyiom.jar"

winlocal = {'java'      : 'C:\\Program Files\\SASHome\\SASPrivateJavaRuntimeEnvironment\\9.4\\jre\\bin\\java',
            'encoding'  : 'windows-1252',
            'classpath' : cpW
            }
Contributor
Posts: 60

Re: Connect to SAS using Python saspy

Posted in reply to ChrisHemedinger

I changed the path for winlocal to point to the correct one and i run:


sas = saspy.SASsession(cfgname='winlocal')

 

and its hanging

 

Community Manager
Posts: 2,955

Re: Connect to SAS using Python saspy

You completed all steps in the configuration doc?  Including:

 

There is one additional requirement. The sspiauth.dll file–also included in your SAS installation–must be in your system PATH environment variable, your java.library.path, or in the home directory of your Java client. You can search for this file in your SAS deployment, though it is likely in SASHome\SASFoundation\9.4\core\sasext.

 

If you add the file to the system PATH environment variable, only list the path to the directory–do not include the file itself. For example:

 

C:\Program Files\SASHome\SASFoundation\9.4\core\sasext

If you adjust the PATH var, you'll need to restart the console that spawned the Python environment you're using -- to pick up the change.

Contributor
Posts: 60

Re: Connect to SAS using Python saspy

Posted in reply to ChrisHemedinger

I did the above:

 

# build out a local classpath variable to use below for Windows clients
cpW = "C:\\Program Files\\SASHome\\SASDeploymentManager\\9.4\\products\\deploywiz__94250__prt__xx__sp0__1\\deploywiz\\sas.svc.connection.jar"
cpW += ";C:\\Program Files\\SASHome\\SASDeploymentManager\\9.4\\products\\deploywiz__94250__prt__xx__sp0__1\\deploywiz\\log4j.jar"
cpW += ";C:\\Program Files\\SASHome\\SASDeploymentManager\\9.4\\products\\deploywiz__94250__prt__xx__sp0__1\\deploywiz\\sas.security.sspi.jar"
cpW += ";C:\\Program Files\\SASHome\\SASDeploymentManager\\9.4\\products\\deploywiz__94250__prt__xx__sp0__1\\deploywiz\\sas.core.jar"
cpW += ";C:\\ProgramData\\Anaconda3\\Lib\\site-packages\\saspy\\java\\saspyiom.jar"

# And, if you've configured IOM to use Encryption, you need these client side jars.
cpW += ";C:\\Program Files\\SASHome\\SASVersionedJarRepository\\eclipse\\plugins\\sas.rutil_904300.0.0.20150204190000_v940m3\\sas.rutil.jar"
cpW += ";C:\\Program Files\\SASHome\\SASVersionedJarRepository\\eclipse\\plugins\\sas.rutil.nls_904300.0.0.20150204190000_v940m3\\sas.rutil.nls.jar"
cpW += ";C:\\Program Files\\SASHome\\SASVersionedJarRepository\\eclipse\\plugins\\sastpj.rutil_6.1.0.0_SAS_20121211183517\\sastpj.rutil.jar"


winlocal = {'java' : 'C:\\Program Files\\SASHome\\SASPrivateJavaRuntimeEnvironment\\9.4\\jre\\bin\\java',
'encoding' : 'windows-1252',
'classpath' : cpW
}

 

added to the PATH env : 

C:\Program Files\SASHome\SASFoundation\9.4\core\sasext

and restarted

 

then tried:

import saspy
import pandas as pd
sas = saspy.SASsession(cfgname='winlocal')

 

and it hungs.

 

Any suggestions?

 

Community Manager
Posts: 2,955

Re: Connect to SAS using Python saspy

Let's go back to the basic checks.

 

  • You can start SAS interactively? (From Start menu or command line)
  • When you try this Python method and it hangs, can you see SAS.EXE in the Windows Task manager process list?

 

Contributor
Posts: 60

Re: Connect to SAS using Python saspy

Posted in reply to ChrisHemedinger

if i run :

 

import saspy
import pandas as pd
SAS_config_names = ['winlocal']
sas = saspy.SASsession(cfgname='winlocal') 

 

it hungs, i can start SAS, sas exe not in the task manager

 

 

If i run:

 

import saspy
import pandas as pd
sas = saspy.SASsession(cfgname='winlocal')

 

i get the following: The SAS Config name specified was not found. Please enter the SAS Config you wish to use. Available Configs are: ['default'] 

 

Community Manager
Posts: 2,955

Re: Connect to SAS using Python saspy

Assuming that it doesn't contain any user/pw info, can you attach your entire ../saspy/sascfg.py file to this thread?  Here, I'll share mine (which might reflect a slightly different install path).

Contributor
Posts: 60

Re: Connect to SAS using Python saspy

Posted in reply to ChrisHemedinger

#
# Copyright SAS Institute
#
# Licensed under the Apache License, Version 2.0 (the License);
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


# Configuration Names for SAS - python List
# This is the list of allowed configuration definitions that can be used. The definition are defined below.
# if there is nore than one name in the list, and cfgname= is not specified in SASsession(), then the user
# will be prompted to choose which configuration to use.
#
# The various options for the different access methods can be specified on the SASsession() i.e.:
# sas = SASsession(cfgname='default', options='-fullstimer', user='me')
#
# Based upon the lock_down configuration option below, you may or may not be able to override option
# that are defined already. Any necessary option (like user, pw for IOM or HTTP) that are not defined will be
# prompted for at run time. To dissallow overrides of as OPTION, when you don't have a value, simply
# specify options=''. This way it's specified so it can't be overridden, even though you don't have any
# specifi value you want applied.
#
#SAS_config_names = ['default', 'ssh', 'iomlinux', 'iomwin', 'winlocal', 'winiomlinux', 'winiomwin', 'http']
#

SAS_config_names=['default']

# Configuration options for pysas - python Dict
# valid key are:
#
# 'lock_down' - True | False. True = Prevent runtime overrides of SAS_Config values below
#

SAS_config_options = {'lock_down': True}

# Configuration options for SAS output. By default output is HTML 5.0 (using "ods html5" statement) but certain templates might not work
# properly with HTML 5.0 so it can also be set to HTML 4.0 instead (using "ods html" statement). This option will only work when using IOM
# in local mode. Note that HTML 4.0 will generate images separately which clutters the workspace and if you download the notebook as HTML,
# the HTML file will need to be put in the same folder as the images for them to appear.
# valid key are:
#
# 'output' = ['html5', 'html']
#
SAS_output_options = {'output' : 'html5'}


# Configuration Definitions
#
# For STDIO and STDIO over SSH access methods
# These need path to SASHome and optional startup options - python Dict
# The default path to the sas start up script is: /opt/sasinside/SASHome/SASFoundation/9.4/sas
# A usual install path is: /opt/sasinside/SASHome
#
# Since python uses utf-8, running SAS with encoding=utf-8 is the expected use case. By default Unix SAS runs in Latin1 (iso-8859-1),
# which does not work well as utf-8. So, transcoding has been implemented in the python layer. The 'encoding' option can be specified to match
# the SAS session encoding (see https://docs.python.org/3.5/library/codecs.html#standard-encodings for python encoding values). latin1 is appropriate
# for the default Unix SAS session encoding
#
# valid keys are:
# 'saspath' - [REQUIRED] path to SAS startup script i.e.: /opt/sasinside/SASHome/SASFoundation/9.4/sas
# 'options' - SAS options to include in the start up command line - Python List
# 'encoding' - This is the python encoding value that matches the SAS session encoding youe SAS session is using
#
# For passwordless ssh connection, the following are also reuqired:
# 'ssh' - [REQUIRED] the ssh command to run
# 'host' - [REQUIRED] the host to connect to
#
default = {'saspath' : '/opt/sasinside/SASHome/SASFoundation/9.4/bin/sas_u8'
}

ssh = {'saspath' : '/opt/sasinside/SASHome/SASFoundation/9.4/bin/sas_en',
'ssh' : '/usr/bin/ssh',
'host' : 'remote.linux.host',
'encoding': 'latin1',
'options' : ["-fullstimer"]
}


# For IOM (Grid Manager or any IOM) and Local Windows via IOM access method
# These configuration definitions are for connecting over IOM. This is designed to be used to connect to a SAS Grid, via Grid Manager
# and also to connect to a local Windows SAS session. The client side (python and java) for this access method can be either Linux or Windows.
# The STDIO access method above is only for Linux. PC SAS requires this IOM interface.
#
# The absence of the iomhost option triggers local Windows SAS mode. In this case none of 'iomhost', 'iomport', 'omruser', 'omrpw' are needed.
# a local SAS session is started up and connected to.
#
# Since python uses utf-8, running SAS with encoding=utf-8 is the expected use case. By default Windows SAS runs in WLatin1 (windows-1252),
# which does not work well as utf-8. So, transcoding has been implemented in the python layer. The 'encoding' option can be specified to match
# the SAS session encoding (see https://docs.python.org/3.5/library/codecs.html#standard-encodings for python encoding values). windows-1252 is appropriate
# for the default Windows SAS session encoding
#
# Since this IOM access method uses the Java IOM client, a classpath is required for the java process to find the necessary jars. Use the template below
# to build out a classpath variable and assign that to the 'classpath' option in the configuration definition. The IOM client jars are delivered as part
# of a Base SAS install, so should be available in any SAS install. The saspyiom.jar is available in the saspy repo/install.
#
# valid keys are:
# 'java' - [REQUIRED] the path to the java executable to use
# 'iomhost' - [REQUIRED for remote IOM case, Don't specify to use a local Windows Session] the resolvable host name, or ip to the IOM server to connect to
# 'iomport' - [REQUIRED for remote IOM case, Don't specify to use a local Windows Session] the port IOM is listening on
# 'authkey' - identifier for user/password credentials to read from .authinfo file. Eliminates prompting for credentials.
# 'omruser' - not suggested [REQUIRED for remote IOM case but PROMTED for at runtime] Don't specify to use a local Windows Session
# 'omrpw' - really not suggested [REQUIRED for remote IOM case but PROMTED for at runtime] Don't specify to use a local Windows Session
# 'encoding' - This is the python encoding value that matches the SAS session encoding of the IOM server you are connecting to
# 'classpath' - [REQUIRED] classpath to IOM client jars and saspy client jar.
# 'appserver' - name ofphysical workspace server (when more than one app server defined in OMR) i.e.: 'SASApp - Workspace Server'


# build out a local classpath variable to use below for Linux clients
cpL = "/opt/sasinside/SASHome/SASDeploymentManager/9.4/products/deploywiz__94400__prt__xx__sp0__1/deploywiz/sas.svc.connection.jar"
cpL += ":/opt/sasinside/SASHome/SASDeploymentManager/9.4/products/deploywiz__94400__prt__xx__sp0__1/deploywiz/log4j.jar"
cpL += ":/opt/sasinside/SASHome/SASDeploymentManager/9.4/products/deploywiz__94400__prt__xx__sp0__1/deploywiz/sas.security.sspi.jar"
cpL += ":/opt/sasinside/SASHome/SASDeploymentManager/9.4/products/deploywiz__94400__prt__xx__sp0__1/deploywiz/sas.core.jar"
cpL += ":/opt/github/saspy/java/saspyiom.jar"

iomlinux = {'java' : '/usr/bin/java',
'iomhost' : 'linux.iom.host',
'iomport' : 8591,
'encoding' : 'latin1',
'classpath' : cpL
}

iomwin = {'java' : '/usr/bin/java',
'iomhost' : 'windows.iom.host',
'iomport' : 8591,
'encoding' : 'windows-1252',
'classpath' : cpL
}


# build out a local classpath variable to use below for Windows clients
cpW = "C:\\Program Files\\SASHome\\SASDeploymentManager\\9.4\\products\\deploywiz__94250__prt__xx__sp0__1\\deploywiz\\sas.svc.connection.jar"
cpW += ";C:\\Program Files\\SASHome\\SASDeploymentManager\\9.4\\products\\deploywiz__94250__prt__xx__sp0__1\\deploywiz\\log4j.jar"
cpW += ";C:\\Program Files\\SASHome\\SASDeploymentManager\\9.4\\products\\deploywiz__94250__prt__xx__sp0__1\\deploywiz\\sas.security.sspi.jar"
cpW += ";C:\\Program Files\\SASHome\\SASDeploymentManager\\9.4\\products\\deploywiz__94250__prt__xx__sp0__1\\deploywiz\\sas.core.jar"
cpW += ";C:\\ProgramData\\Anaconda3\\Lib\\site-packages\\saspy\\java\\saspyiom.jar"

# And, if you've configured IOM to use Encryption, you need these client side jars.
cpW += ";C:\\Program Files\\SASHome\\SASVersionedJarRepository\\eclipse\\plugins\\sas.rutil_904300.0.0.20150204190000_v940m3\\sas.rutil.jar"
cpW += ";C:\\Program Files\\SASHome\\SASVersionedJarRepository\\eclipse\\plugins\\sas.rutil.nls_904300.0.0.20150204190000_v940m3\\sas.rutil.nls.jar"
cpW += ";C:\\Program Files\\SASHome\\SASVersionedJarRepository\\eclipse\\plugins\\sastpj.rutil_6.1.0.0_SAS_20121211183517\\sastpj.rutil.jar"


winlocal = {'java' : 'C:\\Program Files\\SASHome\\SASPrivateJavaRuntimeEnvironment\\9.4\\jre\\bin\\java',
'encoding' : 'windows-1252',
'classpath' : cpW
}

winiomlinux = {'java' : 'java',
'iomhost' : 'linux.iom.host',
'iomport' : 8591,
'encoding' : 'latin1',
'classpath' : cpW
}

winiomwin = {'java' : 'C:\\Program Files\\SASHome\\SASPrivateJavaRuntimeEnvironment\\9.4\\jre\\bin\\java',
'iomhost' : 'windows.iom.host',
'iomport' : 8591,
'encoding' : 'windows-1252',
'classpath' : cpW
}


# Future - for the HTTP access method to connect to the Compute Service
# This access method is not available yet.
#
#
# These need ip addr and port, other values will be prompted for - python Dict
# valid keys are:
# 'ip' - [REQUIRED] host address
# 'port' - [REQUIRED] port; the code Defaults this to 80 (the Compute Services default port)
# 'context' - context name defined on the compute service [PROMTED for at runtime if more than one defined]
# 'options' - SAS options to include (no '-' (dashes), just option names and values)
# 'user' - not suggested [REQUIRED but PROMTED for at runtime]
# 'pw' - really not suggested [REQUIRED but PROMTED for at runtime]
#
#

http = {'ip' : 'host.running.compute.service',
'port' : 80,
'context' : 'Tom2'
}

 

Community Manager
Posts: 2,955

Re: Connect to SAS using Python saspy

So I would definitely change this line to add winlocal:

SAS_config_names=['default', 'winlocal']

 

And...not sure if will make a difference, but change lock_down to false:

SAS_config_options = {'lock_down': False}

 

Might need @Jared to also take a look or provide additional diagnostic tips.

Contributor
Posts: 60

Re: Connect to SAS using Python saspy

Posted in reply to ChrisHemedinger

Made the changes and now i get the following:

 

Java Error:
Error: Could not find or load main class pyiom.saspy2j


Subprocess failed to start. Double check you settings in sascfg.py file.

Attempted to run program C:\Program Files\SASHome\SASPrivateJavaRuntimeEnvironment\9.4\jre\bin\java with the following parameters:['C:\\Program Files\\SASHome\\SASPrivateJavaRuntimeEnvironment\\9.4\\jre\\bin\\java', '-classpath', 'C:\\Program Files\\SASHome\\SASDeploymentManager\\9.4\\products\\deploywiz__94250__prt__xx__sp0__1\\deploywiz\\sas.svc.connection.jar;C:\\Program Files\\SASHome\\SASDeploymentManager\\9.4\\products\\deploywiz__94250__prt__xx__sp0__1\\deploywiz\\log4j.jar;C:\\Program Files\\SASHome\\SASDeploymentManager\\9.4\\products\\deploywiz__94250__prt__xx__sp0__1\\deploywiz\\sas.security.sspi.jar;C:\\Program Files\\SASHome\\SASDeploymentManager\\9.4\\products\\deploywiz__94250__prt__xx__sp0__1\\deploywiz\\sas.core.jar;C:\\ProgramData\\Anaconda3\\Lib\\site-packages\\saspy\\java\\saspyiom.jar;C:\\Program Files\\SASHome\\SASVersionedJarRepository\\eclipse\\plugins\\sas.rutil_904300.0.0.20150204190000_v940m3\\sas.rutil.jar;C:\\Program Files\\SASHome\\SASVersionedJarRepository\\eclipse\\plugins\\sas.rutil.nls_904300.0.0.20150204190000_v940m3\\sas.rutil.nls.jar;C:\\Program Files\\SASHome\\SASVersionedJarRepository\\eclipse\\plugins\\sastpj.rutil_6.1.0.0_SAS_20121211183517\\sastpj.rutil.jar', 'pyiom.saspy2j', '-host', 'localhost', '-stdinport', '52234', '-stdoutport', '52235', '-stderrport', '52236', '-zero', '']

If no Java Error above, try running the following command (where saspy is running) manually to see if it's a problem starting Java:
C:\Program Files\SASHome\SASPrivateJavaRuntimeEnvironment\9.4\jre\bin\java -classpath "C:\Program Files\SASHome\SASDeploymentManager\9.4\products\deploywiz__94250__prt__xx__sp0__1\deploywiz\sas.svc.connection.jar;C:\Program Files\SASHome\SASDeploymentManager\9.4\products\deploywiz__94250__prt__xx__sp0__1\deploywiz\log4j.jar;C:\Program Files\SASHome\SASDeploymentManager\9.4\products\deploywiz__94250__prt__xx__sp0__1\deploywiz\sas.security.sspi.jar;C:\Program Files\SASHome\SASDeploymentManager\9.4\products\deploywiz__94250__prt__xx__sp0__1\deploywiz\sas.core.jar;C:\ProgramData\Anaconda3\Lib\site-packages\saspy\java\saspyiom.jar;C:\Program Files\SASHome\SASVersionedJarRepository\eclipse\plugins\sas.rutil_904300.0.0.20150204190000_v940m3\sas.rutil.jar;C:\Program Files\SASHome\SASVersionedJarRepository\eclipse\plugins\sas.rutil.nls_904300.0.0.20150204190000_v940m3\sas.rutil.nls.jar;C:\Program Files\SASHome\SASVersionedJarRepository\eclipse\plugins\sastpj.rutil_6.1.0.0_SAS_20121211183517\sastpj.rutil.jar" pyiom.saspy2j -host localhost -stdinport 52234 -stdoutport 52235 -stderrport 52236 -zero

No SAS process attached. SAS process has terminated unexpectedly.
No SAS process attached. SAS process has terminated unexpectedly.
No SAS process attached. SAS process has terminated unexpectedly.

SAS Employee
Posts: 13

Re: Connect to SAS using Python saspy

Posted in reply to ChrisHemedinger

The shipped version of the sascfg.py includes configuration details for the various connection methods for saspy. These are guidelines and likely your environment will require some modifications.

The latest version of the file can be viewed on GitHub

The documentation has configuration section that walks through the options step by step. If you have feedback or enhancements I welcome your input.

 

Here are a few common items to check (line 35)

  • SAS_config_names=['default']
    this line is a python list of the available configuration names. If there is only one configuration name then saspy will use that, if there are multiple and not explicitly specified the user will be prompted. A winlocal reference configuration is included in the file, to use it change the above line to this and saspy will automatically attempt to connect to your local PC SAS version. winlocal is just a name that represents a python dictionary 
    SAS_config_names=['winlocal']

 or if you're connecting to multiple SAS servers then you might want to have something like this:

 

SAS_config_names=['winlocal', 'zos', 'viya']

 

  • To connect using IOM, you will need to have java installed where python (or Jupyter) will be run and several SAS jars that came with your deployment. On PC SAS, that will just be a matter of finding the location on your system (line 145 shows a default path for 9.4m3 install yours will likely be different). If you're connecting to a remote server via IOM then you'll need to copy the jars from one system to another.

 

  • lock_down. This prevents users from overriding configuration options. For PC SAS this won't really matter, but for a server installation I would set it to True (the default). Here is the inline documentation beginning at line 26
# Based upon the lock_down configuration option below, you may or may not be able to override option
# that are defined already. Any necessary option (like user, pw for IOM or HTTP) that are not defined will be 
# prompted for at run time. To dissallow overrides of as OPTION, when you don't have a value, simply
# specify options=''. This way it's specified so it can't be overridden, even though you don't have any
# specific value you want applied.

 

 Jared

Ask a Question
Discussion stats
  • 15 replies
  • 413 views
  • 9 likes
  • 4 in conversation