- Difficulty level: easy
- Time need to lean: 10 minutes or less
- Key points:
- You can only exchanged SAS datasets as dataframes with magics
%get
and%put
. - SAS statements can start with
%
, you will have to prefix them with a new line to avoid being handled as SoS magics. - If the dataset if not in the
WORK
library, you will have to pass the SAS dataset with namelibname.dataset
, and the dataset will be obtained as a dataframe with namelibname_dataset
.
- You can only exchanged SAS datasets as dataframes with magics
To use SAS with SoS, you will need to setup sos-kernel
and saspy
. The trickiest part is to the creation of a sascfg_personal.py
file that specifies how to connect to the SAS server from the Jupyter server.
After you set up a sos-kernel
, verified that the sas
kernel appears in the list of jupyter kernelspec list
, you should start a Jupyter notebook with a SAS
kernel and verify if you can execute SAS
codes from the Jupyter notebook. Then you can install sos-sas
with command
pip install sos-sas
and then select SAS
as the language for SAS cells.
A SAS dataset is roughly equivalent to Python's DataFrame (Pandas) and R's dataframe, and this is the only data type that can be exchanged between SAS and other languages.
Data frames in languages such as Python and R can be send to SAS with the usual %get
or %put
magic. For example, you can %get
a dataset from R as follows:
As you can see, SoS basically creates a dataset MTCARS
in the WORK
library. There is no way for you to specify name of a library so you will have to copy the datasets to a library in SAS if you would like to save the dataset permanently.
The following example shows the passing of a Python Pandas dataframe to SAS and plot it there. The data is of medium size of 100k rows, but should take just a few seconds to be passed to SAS.
If you have a SAS dataset, for example, a MYCLASS
dataset as follows
You can transfer the dataset to Python or R and analyze there. Note that SAS dataset is case insensitive so you can get either myclass
or MYCLASS
, but the variable name will be case sensitive in Python.
If the data is not in the WORK
library, you will have to specify library name in the %get
(or %put
magic). For example, you can get the sashelp.air
dataset from SAS with name sashelp.air
. However, because this is not a valid name for a single variable, the dataset is obtained as sashelp_air
.
The SAS language has syntaxes that look like Jupyter magics. To avoid SoS treating them as SoS magics, you will have to prefix the statement with a new line. For example, to execute a SAS %put
statement as the first statement in a cell, you will have to enter it as the second line.
The sas-kernel
generates output in HTML format, with embedded CSS (style sheet). Although the CSS is designed only for SAS outputs, they can change the look and feel of the entire SoS Notebook, even the HTML report generated from SoS Notebooks with SAS outputs. For example, the page you are reading looks a bit funny with elements (e.g. table of contents) look different from other pages, because of these embedded CSS.
SoS %get
datasets from SAS by importing data from the SAS data files (.sas7bdat
files) directly. This is not possible if the SAS server is on a remote windows server that is connected by the sas-kernel
via the IOM method. In another word, getting datasets from SAS only works if the SAS and Jupyter servers share the same server or file system, or connected via SSH
.
The %cd
magic changes the current working directory on the SAS server, which does not have to be the same server on which the Jupyter server resides. The %cd
magic will fail to change directory if the specified path does not exist on the remote server.