Jupyter Notebook

4月 182022
 

SASPy and the SAS kernel for Jupyter Notebook enable you to connect to SAS® 9.4 and the SAS® Viya® compute server from your client machine. You can then interact with SAS and SAS Viya via Python. If you are not familiar with SASPy and the SAS kernel, this article provides an introduction.

Installing SASPy

SASPy is a client application. You install SASPy on the same client where Python is installed. If SAS is installed on a server, you do not need to install SASPy on the server (because it needs to be installed only on the client).

Since most users install Python, SASPy, and the SAS kernel on a Microsoft Windows client, this article assumes that your client is a Windows client too. But your client could also be a Linux or UNIX environment.

In most situations, installing SASPy on your client machine is as simple as issuing a Python PIP command on your client. For example, if your client is a Windows client, issue this command from a DOS prompt or DOS command line:

pip install saspy

To verify that SASPy is properly installed, submit the following client command:

pip show saspy

The information that is displayed by the pip show saspy command also tells you which version of SASPy is installed and the client directory in which SASPy is installed.

Installing the SAS kernel

The SAS kernel is also a client application. You install the SAS kernel on the same client where Python and SASPy are installed. Note that while the use of SASPy does not require the SAS kernel, the SAS kernel does require SASPy to be installed. Also, in order to use the SAS kernel, you must first be able to use SASPy to connect successfully to SAS or SAS Viya.

In most situations, installing the SAS kernel on your client machine is as simple as issuing a Python PIP command. For example, if your client is a Windows client, issue this command from a DOS prompt or DOS command line:

pip install sas_kernel

To verify that the SAS kernel is properly installed, submit the following client command:

pip show sas_kernel

The information that is displayed by the pip show sas_kernel command also tells you which version of the SAS kernel is installed and the client directory in which the SAS kernel is installed.

Python and Java dependencies

SASPy and the SAS kernel require Python 3.4 or higher.

Also, most configurations that use SAS 9.4 require that Java be installed on your client. To check to see whether you have Java on your client, issue the following command:

java -version

Common operating environments

This article discusses several commonly used operating environments that use SASPy and the SAS kernel:

  • A local SAS session that is running on Windows, where both SAS and SASPy are installed on the same local Windows client machine
  • A remote SAS session that is running on a Windows server
  • A remote SAS session that is running on a Linux server
  • A SAS Programming Runtime Environment (SPRE) compute server that is running as part of a SAS Viya installation

Connecting to a local SAS session that is running on Windows

A common scenario is to use SASPy with a local installation of SAS on Windows. In this case, use the WINLOCAL definition in your sascfg.py configuration file. The configuration file resides in your SASPy installation directory. To determine the exact name of that directory, issue the following command on your client machine:

pip show saspy

To use the WINLOCAL definition, find the following line in your sascfg.py file:

SAS_config_names=['default']

Modify the line to match the following:

SAS_config_names=['winlocal']

To connect to a local Windows installation of SAS, submit the following Python statements:

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

Connecting to a remote SAS session that is running on a Windows server

Another common scenario is to use SASPy on your client machine to connect to SAS 9.4, which is running on a separate Windows server. In this case, use the WINIOMWIN definition in your sascfg.py configuration file.

To use the WINIOMWIN definition, find the following line in your sascfg.py file:

SAS_config_names=['default']

Modify the line to match the following:

SAS_config_names=['winiomwin']

Then, farther down in your sascfg.py file, find the following default definition:

winiomwin  = {'java'    : 'java',
            'iomhost'   : 'windows.iom.host',
            'iomport'   : 8591,
            }

Modify the value for IOMHOST in this definition by specifying the host name of the SAS IOM server that is running on Windows.

Then, to connect to SAS 9.4 on your Windows server, submit the following Python statements from your client:

import saspy
import pandas as pd
sas = saspy.SASsession(cfgname="winiomwin")

Connecting to a remote SAS session that is running on a Linux server

You might use SASPy on your client machine to connect to SAS 9.4, which is running on a Linux server. In this case, use the WINIOMLINUX definition in your sascfg.py configuration file.

To use the WINIOMLINUX definition, find the following line in your sascfg.py file:

SAS_config_names=['default']

Modify the line to match the following:

SAS_config_names=['winiomlinux']

Then, farther down in your sascfg.py file, find the following default definition:

winiomlinux = {'java'   : 'java',
            'iomhost'   : 'linux.iom.host',
            'iomport'   : 8591,
            }

Modify the value for IOMHOST in this definition by specifying the host name of the SAS IOM server that is running on Linux.

Then, to connect to SAS 9.4 on your Linux server, submit the following Python statements from your client:

import saspy
import pandas as pd
sas = saspy.SASsession(cfgname="winiomlinux")

Connecting to a SPRE server that is running as part of SAS Viya

SASPy cannot connect directly to a SAS® Cloud Analytic Services (CAS) server that is running as part of SAS Viya. However, SASPy can connect to the SPRE server that is running as part of SAS Viya. (The SPRE server is also referred to as the compute server in SAS Viya.) In this case, use the HTTPVIYA definition in your sascfg.py configuration file.

To use the HTTPVIYA definition, find the following line in your sascfg.py file:

SAS_config_names=['default']

Modify the line to match the following:

SAS_config_names=['httpviya']

Then, farther down in your sascfg.py file, find the following default definition:

httpviya = {'ip'      : 'name.of.your.spre.server',
            'ssl'     : False,  # this will use port 80
            'context' : 'Data Mining compute context',
            'authkey' : 'viya_user-pw',
            'options' : ["fullstimer", "memsize=1G"]
            }

Modify the IP value in this definition by specifying the name of the SPRE server for your installation of SAS Viya.

Then, to connect to the SPRE server, submit the following Python statements from your client:

import saspy
import pandas as pd
sas = saspy.SASsession(cfgname="httpviya")

Using SASPy

After you connect successfully to SAS or the SPRE server, you can then submit Python statements via your client to interact with SAS. For example, you can display the first five rows (observations) in SASHELP.CLASS by submitting the following statements:

cars = sas.sasdata("cars","sashelp")
cars.head()

You can display summary statistics for SASHELP.CLASS with the following code:

cars = sas.sasdata("cars","sashelp")
cars.describe()

Using the SAS kernel for Jupyter Notebook

The SAS kernel enables you to use the Jupyter Notebook interface to execute SAS code and view results inline.

As previously noted, the SAS kernel requires SASPy to be installed. You also must be able to connect successfully to SAS or SAS Viya via SASPy. After you do so, after bringing up Jupyter Notebook, in the upper right-hand corner of your notebook, choose New > SAS.

After that, you can then submit regular SAS statements via Jupyter Notebook. Here is an example:

proc print data=sashelp.class;
run;

Additional resources and documentation

For additional information about using SASPy and the SAS kernel, see these resources:

Getting started using SASPy® and the SAS® kernel for Jupyter Notebook was published on SAS Users.

8月 112016
 
Jupyter notebook does not support Table Of Contents (ToC) out of box, unlike R Markdown. There are nbextensions that ppl made to support this. It is easy to add this capability, following these steps to add Table of Contents to your notebook:

 1. Find your Jupyter Notebook data directory by using this command in your DOS console:
          a. Jupyter --data-dir

2. Copy the following two files into your Jupyter data directory:
           https://rawgithub.com/minrk/ipython_extensions/master/nbextensions/toc.js
           https://rawgithub.com/minrk/ipython_extensions/master/nbextensions/toc.css

3. Run this command:
           a. Jupyter nbextension enable toc --user

4. In your Jupyter Notebook, add this command to a MarkDown cell on top of your notebook:
           a. < div id="toc" > < /div >

One caveat is that all headings will be listed in ToC, including your Title (if there is one). One easy work around is to write your title in Word, save a screenshot into an image, save the image file to the same folder as your notebook is, then use the following command in the top Markdown cell:

 < div style="float: left;" > < img src="Title.png" height="200" width="800"> < /div >
 Posted by at 7:13 上午
5月 112016
 

SASViyaI’ve been giving presentations about SAS® ViyaTM for a couple of months now, and the reactions have been positive. I’m part of a much bigger cast of speakers at SAS who talk about the new analytics platform with key customers and analysts. While some presenters focus on the overarching benefits or the integration with previous versions of SAS, I get to focus on hands-on demonstrations. My style is to show SAS Viya technology in use, which helps attendees understand the different user experiences.

The demonstrations I show use anywhere from 90,000 to 2.5 million observations. Sometimes I’m looking for fraud or sometimes I’m looking for opportunities to reduce attrition. No matter what business problem I’m trying to solve, I like to show four different ways to tackle the problem, depending on the user persona and skillsets.

First, I show SAS® Visual Statistics. This is a suitable interface for business analysts and citizen data scientists. I can point and click to do a logistic regression and find an answer. Or, I can start to explore my data with SAS® Visual Analytics before I do any modeling.

Running a logistic regression in SAS Visual Statistics

Running a logistic regression in SAS Visual Analytics (click to enlarge).

Next, I show SAS® Studio, where you can also point and click, or you can program SAS code. You can do both here, or toggle back and forth, which provides a lot of benefits when reviewing how your code works. Here, I run a logistic regression, write the procedure and use PROC LOGSELECT, which is the logistic procedure in SAS Viya. I also point out all of the exploratory and descriptive tasks and procedures you can use before fitting your model.

StudioLogistic (002)

SAS Studio shows the selected model and the code side by side (click to enlarge).

Then, I tab over to Jupyter Notebook and show the CAS Python API. (CAS is short for cloud analytic server.) I can write Python code that calls specific CAS actions, like calling the logistic action set or performing model assessment. The CAS Python API is not yet released but will be available later this year.

SAS code in Python

Performing a model assessment from inside Jupyter Notebook.

Finally, I like to show these prepackaged predictive models that run SAS through APIs. Using analytics as a service by SAS, these APIs can be used to embed logistic regression or other modeling techniques into any other application.

A3s (002)

APIs are portable pieces of code that can be easily combined and stacked together to enhance other applications or websites (click to enlarge).

I use the same data for all of the examples and because the same CAS action set is used for analysis you get the same results. For each one, SAS Viya goes right to the data and lifts it up into memory. I can perform my analyses quickly and nimbly, and when I’m done, the data dribbles back down to its initial location instead of proliferating copies of the data on hard disks.

Not only is it quick and easy to get complex answers – but you can get them in whatever way feels most comfortable to you.

Which of the four do you prefer? And what ideas do the different options inspire in you?

tags: Jupyter Notebook, Python, SAS Viya

Choose your own adventure with SAS Viya was published on SAS Users.