SASpy
SASpy
This page demonstrates how to connect Python to SAS using the SASPy library. We will show how to configure the connection, establish a session, and submit SAS code from Python.
The following packages are required:
Package | Comment |
---|---|
saspy | Main Python library for connecting to SAS. |
pandas | For handling dataframes (optional, but common). |
Below is an example SASPy configuration block:
# The names of the SAS configurations to use
SAS_config_names = ["iomcom"]
# Configuration block for the IOM connection
iomcom = {
"iomhost": "your_sas_server_name_or_ip",
"iomport": 8591,
"iomuser": "your_username",
"iompwd": "your_password",
"provider": "sas.iomprovider"
}
# Configuration options applied globally to the SAS connection
SAS_config_options = {
"lock_down": True,
"verbose": True
}
After defining parameters, you can establish a connection and submit SAS code. Here is a class-based example:
import saspy
class SASSession:
def __init__(self):
"""Initializes the SAS session using the predefined configuration."""
self.sas = None
self._initialize_session()
def _initialize_session(self):
try:
self.sas = saspy.SASsession()
print("SAS session initialized successfully.")
except Exception as e:
print(f"Error initializing SAS session: {e}")
self.sas = None
def submit(self, sas_code):
"""Submits SAS code to the session and returns the result."""
if self.sas:
result = self.sas.submit(sas_code)
sas_log = result.get('LOG', 'No LOG output')
sas_lst = result.get('LST', 'No LST output')
return {'LOG': sas_log, 'LST': sas_lst}
else:
print("SAS session is not initialized.")
return None
def close(self):
"""Closes the SAS session."""
if self.sas:
self.sas._endsas()
print("SAS session closed.")
else:
print("SAS session is not open.")
# Example usage
if __name__ == "__main__":
sas_session = SASSession()
result = sas_session.submit("proc print data=sashelp.class; run;")
if result:
print("SAS LOG:", result['LOG'])
print("SAS OUTPUT (LST):", result['LST'])
sas_session.close()
For more details, see the SASPy documentation.
This guide walks you through creating a lightweight Conda environment with Python 3.12.3 on Ubuntu 24, suitable for low-RAM laptops and basic data analysis tasks with Microsoft-friendly packages.
Conda Environment File
name: light-data-env
channels:
- defaults
- conda-forge
dependencies:
- python=3.13.5
- pandas
- numpy
- matplotlib
- seaborn
- openpyxl
- xlrd
- requests
- jupyterlab
- statsmodels
- scikit-learn
- pip
- pip:
- pywin32
- selenium
Step 1: Install Miniconda
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh
Step 2: Create the Environment
conda env create -f light-data-env.yml
Step 3: Activate the Environment
conda activate light-data-env
Step 4: Launch Jupyter Lab
jupyter lab
Notes
- Python 3.12.3 is stable with all included packages as of mid-2024.
xlwings
on Linux supports Excel file handling but not live Excel application control (Windows-only feature).
- Replace
jupyterlab
withnotebook
for a faster interface. - Remove
scikit-learn
if machine learning is not required. - Omit
xlwings
if you do not need advanced Excel automation.
This guide helps you install essential VS Code extensions for Python data analysis, Conda environment management, and Microsoft-friendly workflows.
Essential Extensions
- Python -
ms-python.python
- Jupyter -
ms-toolsai.jupyter
- Pylance -
ms-python.vscode-pylance
- Conda -
ms-python.conda
- Jupyter Keymap -
ms-toolsai.jupyter-keymap
- Jupyter Renderers -
ms-toolsai.jupyter-renderers
- Code Runner -
formulahendry.code-runner
- Path Intellisense -
christian-kohler.path-intellisense
- Bracket Pair Colorizer 2 -
coenraads.bracket-pair-colorizer-2
- Excel Viewer -
GrapeCity.gc-excelviewer
- GitLens -
eamodio.gitlens
Bash Script for Quick Installation
#!/bin/bash
# VS Code Essential Extensions Installer for Windows (PowerShell)
Write-Output "Installing VS Code Extensions..."
# Python Core
code --install-extension ms-python.python
code --install-extension ms-toolsai.jupyter
code --install-extension ms-python.vscode-pylance
# Jupyter Helpers
code --install-extension ms-toolsai.jupyter-keymap
code --install-extension ms-toolsai.jupyter-renderers
# Productivity Tools
code --install-extension formulahendry.code-runner
code --install-extension christian-kohler.path-intellisense
# Data Science Extensions
code --install-extension ms-toolsai.vscode-jupyter-slideshow
code --install-extension ms-python.anaconda-extension-pack
code --install-extension njpwerner.autodocstring
code --install-extension kevinrose.vsc-python-indent
code --install-extension msrvida.vscode-sanddance
code --install-extension ms-toolsai.datawrangler
code --install-extension ms-toolsai.jupyter-renderers
code --install-extension ms-toolsai.jupyter-keymap
# PHP Extensions
code --install-extension felixfbecker.php-debug
code --install-extension felixfbecker.php-intellisense
code --install-extension bmewburn.vscode-intelephense-client
code --install-extension mehedidracula.php-namespace-resolver
code --install-extension neilbrayfield.php-docblocker
# General Web Development
code --install-extension esbenp.prettier-vscode
code --install-extension dbaeumer.vscode-eslint
# Version Control
code --install-extension eamodio.gitlens
Write-Output "VS Code Extensions installed successfully."
Pause
# Version Control
code --install-extension eamodio.gitlens
Write-Output "VS Code Extensions installed successfully."
Pause
Usage
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
.\vscode-setup.ps1
code
command is available in your terminal. In VS Code, press Ctrl+Shift+P
and run Shell Command: Install 'code' command in PATH if it's not already set.