Configure SAS Kernel for Jupyter
Setup guide for running SAS code in Jupyter notebooks
1 Overview
This guide shows how to configure Jupyter to run SAS code in .ipynb notebooks, enabling interactive SAS programming with inline output display.
2 Prerequisites
- SAS installed on your system (SAS 9.4 or later recommended)
- Python 3.8+ with Jupyter installed
- Admin/elevated privileges for kernel installation
3 Installation Steps
3.1 1. Install SAS Kernel for Jupyter
Open a command prompt or PowerShell with admin rights and run:
pip install saspy
pip install sas_kernel3.2 2. Configure SAS Connection
Create a configuration file at ~/.saspy/sascfg_personal.py (on Windows: C:\Users\YourUsername\.saspy\sascfg_personal.py):
# SAS configuration for local installation
SAS_config_names = ['winlocal']
winlocal = {
'saspath': 'C:/Program Files/SASHome/SASFoundation/9.4/sas.exe',
'options': ['-nosplash', '-noterminal'],
'encoding': 'utf-8'
}
# Adjust the 'saspath' to match your SAS installation pathImportant: Update the saspath to your actual SAS installation path.
3.3 3. Install the Kernel
After installing sas_kernel, register it with Jupyter:
jupyter kernelspec install --user --name sasOr if already registered, you can update it:
python -m sas_kernel.install3.4 4. Verify Installation
Check that SAS kernel is available:
jupyter kernelspec listYou should see sas in the list of available kernels.
4 Using SAS in Quarto/Jupyter
4.1 Create a SAS Notebook
In your .ipynb file, set the kernel to SAS. In the notebook metadata, add:
{
"kernelspec": {
"display_name": "SAS",
"language": "sas",
"name": "sas"
}
}Or simply select “SAS” from the kernel dropdown when creating a new notebook in Jupyter or VS Code.
4.2 Example SAS Code Cell
/* Create a sample dataset */
data class;
set sashelp.class;
bmi = weight / (height**2) * 703;
run;
/* Display summary statistics */
proc means data=class;
var age height weight bmi;
run;
4.3 Using in Quarto Documents
You can embed .ipynb files directly in Quarto:
- Create your notebook:
fundamentals/my-lesson.ipynb - Reference it in your
.qmdfile or include it in the listing
Or execute SAS code directly in .qmd files using the {sas} engine:
```{sas}
proc print data=sashelp.class;
run;
```Note: Direct SAS execution in .qmd requires additional Quarto SAS engine configuration.
5 Alternative: SASPy Configuration
If you prefer using SASPy without the kernel (for Python-controlled SAS sessions):
import saspy
sas = saspy.SASsession(cfgname='winlocal')
# Submit SAS code
sas.submit('''
data test;
x = 1;
y = 2;
run;
''')
# Get dataset as pandas DataFrame
df = sas.sd2df('sashelp.class')
print(df.head())6 Troubleshooting
6.1 Kernel not showing up
- Restart VS Code/Jupyter after installation
- Check kernel list:
jupyter kernelspec list - Reinstall:
python -m sas_kernel.install --user
6.2 Connection errors
- Verify SAS path in
sascfg_personal.py - Check SAS license is active
- Ensure SAS runs from command line:
"C:\Path\To\sas.exe" -nodms
6.3 Encoding issues
- Set encoding in config:
'encoding': 'utf-8'or'latin1' - Check your data file encoding
7 Next Steps
- Create practice notebooks under
fundamentals/ - Use
.ipynbfor interactive exercises - Use
.qmdfor lesson content with embedded code examples - See Course Materials for structured lessons