Running SAS from Jupyter with SASPy
Setting up SASPy so that SAS code runs from Jupyter Notebook or JupyterLab on Windows, with a working configuration and the fix for the most common authentication error.
SASPy lets Python talk to a SAS session, so SAS code can run inside Jupyter next to pandas and plotting code. This guide covers a local Windows installation of SAS 9.4. By the end you should be able to start a SAS session from a notebook and run a procedure against a SAS data set.
Prerequisites
Install JupyterLab and SASPy into the Python environment you use for notebooks:
pip install jupyterlab saspy
Configuration
1. Create a personal configuration file. SASPy reads its connection settings from sascfg_personal.py. Start from the example below and change the paths to match your SAS installation. Only the winlocal entry is needed for a local Windows setup; the other entries show the shapes used for remote and Viya connections.
2. Make sure SAS is on the PATH. The SAS executable and its sasext directory need to be discoverable. The configuration below appends them for the Python process.
3. Check the hosts file. Integrated Windows authentication can fail if localhost is not listed explicitly (see Troubleshooting).
Configuration template
# sascfg_personal.py. Change every path to match your installation.
# Reference: https://sassoftware.github.io/saspy/configuration.html
SAS_config_names = ['winlocal']
SAS_config_options = {
'lock_down': False,
'verbose': True,
'prompt': True,
}
SAS_output_options = {
'output': 'html5',
'style': 'HTMLBlue',
}
# Local SAS on Windows, using the IOM connection through Java.
cpW = "C:\\Program Files\\SASHome\\SASDeploymentManager\\9.4\\products\\deploywiz__94532__prt__xx__sp0__1\\deploywiz\\sas.svc.connection.jar"
cpW += ";C:\\Program Files\\SASHome\\SASDeploymentManager\\9.4\\products\\deploywiz__94532__prt__xx__sp0__1\\deploywiz\\log4j.jar"
cpW += ";C:\\Program Files\\SASHome\\SASDeploymentManager\\9.4\\products\\deploywiz__94532__prt__xx__sp0__1\\deploywiz\\sas.security.sspi.jar"
cpW += ";C:\\Program Files\\SASHome\\SASDeploymentManager\\9.4\\products\\deploywiz__94532__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.exe',
'encoding': 'windows-1252',
'classpath': cpW,
}
# Other connection types, for reference.
ssh = {
'saspath': '/opt/sasinside/SASHome/SASFoundation/9.4/bin/sas_en',
'ssh': '/usr/bin/ssh',
'host': 'remote.linux.host',
'encoding': 'latin1',
'options': ['-fullstimer'],
}
winiomIWA = {
'java': 'java',
'iomhost': 'windows.iom.host',
'iomport': 8591,
'sspi': True,
}
httpsviya = {
'url': 'https://viya.deployment.com',
'context': 'Data Mining compute context',
'authkey': 'viya_user-pw',
'options': ['fullstimer', 'memsize=1G'],
}
import os
os.environ["PATH"] += ";C:\\Program Files\\SASHome\\SASFoundation\\9.4\\core\\sasext\\sspiauth.dll"
os.environ["PATH"] += ";C:\\Program Files\\SASHome\\SASFoundation\\9.4\\core\\sasext"
The saspyiom.jar path depends on where SASPy is installed. In a virtual environment it sits under that environment’s site-packages\saspy\java folder rather than the Anaconda path shown.
Check that it works
import saspy
sas = saspy.SASsession(cfgname='winlocal')
cars = sas.sasdata('CARS', 'SASHELP')
cars.describe()
If the session starts, describe() prints summary statistics for SASHELP.CARS, and you can submit any SAS code with sas.submit() or the %%SAS cell magic.
Troubleshooting
Error: The application could not log on to the server "localhost:0". Integrated Windows authentication failed.
-
Read the IOM section of the SASPy troubleshooting guide and SAS Usage Note 55227.
-
Open
C:\Windows\System32\drivers\etc\hostsas an administrator and change127.0.0.1 view-localhostto
127.0.0.1 localhost view-localhost -
Restart the Python kernel and create the session again.
First published on my earlier site, andeeli.github.io/posts/Set-Up-saspy/, and revised for this one.