A question came through the SAS Communities a few weeks ago asking, "Can we connect SAS Viya for learners from macOS terminal via Python SWAT library?" The answer is, "Yes". In this article, I'll cover how users can connect to SAS Viya using the SWAT package.
First, I'd like to point out that there is an easier (and recommended) way to make the connection outlined in this article. SAS Viya for Learners users have access to a Jupyter environment that is directly linked to the SAS Viya instance. Add /hub to the server URL. This launches a Juypter hub. Once in the hub, create a notebook and run the following commands:
import swat, os
conn = swat.CAS(os.environ['CAS_CONTROLLER'], 5570, password=os.environ['ACCESS_TOKEN'])
library(swat)
conn <- swat::CAS(Sys.getenv('CAS_CONTROLLER'), 5570, password=Sys.getenv('ACCESS_TOKEN'))
And Voilà, you're connected. This article will only cover the remote connection for Python. If there's a need for an R equivalent, please leave a comment.
Users may have reasons for not following this simple procedure, so I'll explain how to make the SWAT connection from a local Jupyter Notebook. The steps and commands are slightly different for Windows users vs Mac and Linux. I'll cover both below.
Follow these steps to make the SWAT connection. These steps MUST be carried out using Windows PowerShell. They will not work in the Command Prompt, Git Bash, or any other shell.
You will need to extract out the public certificate. To do this, I did the following replacing <server.demo.sas.com> with actual value:
echo -n | openssl s_client -showcerts -connect <server.demo.sas.com>:443 -servername <server.demo.sas.com>:443 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > certificate.pem
Please note that you need to remove the https:// from the server name, in both instances. This copies the Viya certificate to your local home directory. Normally this will be in C:\users\<your user name>.
For Viya for Learners 4, you will be using single sign on. As a result, you will need to extract your access token:
[Environment]::SetEnvironmentVariable('ACCESS_TOKEN','eyJhbGciOiJSUzI1NiIsImprdSI6Imh0d.......','user')
This step must be done in a new Windows Powershell window.
Jupyter Lab
In the Jupyter lab, upload the certificate.pem and Remote_SWAT_Connection_Win.ipynb Jupyter notebook to either the home directory or a directory of your choice.
Open the Remote_SWAT_Connection_Win.ipynb Jupyter notebook. In the first cell, fill in the SAS Viya server URL and file_path. Run the cell to create the variables. The second cell makes the connection to SWAT and prints out the connection information.
Below is a representation of the experience in Jupyter.
Please note that the access token is good for an hour. You'll have to run through the process again after that time. This is another reason it is recommended you use the Jupyter hub attached to the SAS Viya environment to make SWAT connections.
Follow these steps to make the SWAT connection in a zsh shell.
You will need to extract out the public certificate. To do this, I did the following replacing <server.demo.sas.com> with actual value:
echo -n | openssl s_client -showcerts -connect <server.demo.sas.com>:443 -servername <server.demo.sas.com>:443 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > certificate.pem
Please note that you need to remove the https:// from the server name, in both instances. This copies the Viya certificate to your local home directory. Normally for the Mac this will be in /Users/<your user name>. For Linux it will be /home/<your user name>.
For Viya for Learners 4, you will be using single sign on. As a result, you will need to extract your access token:
export ACCESS_TOKEN=eyJh9iiskgK…
Jupyter Lab
In the Jupyter lab, upload the certificate.pem and Remote_SWAT_Connection_Mac_Linux.ipynb Jupyter notebook to either the home directory or a directory of your choice.
Open the Remote_SWAT_Connection_Mac_Linux.ipynb Jupyter notebook. In the first cell, fill in the SAS Viya server URL and file_path. Run the cell to create the variables. The second cell makes the connection to SWAT and prints out the connection information.
Below is a representation of the experience in Jupyter for Mac OS. Linux would be the same output except /Users/... would be replaced with /home/...
Note that the access token is good for 10 hours. You'll have to run through the process again after that time. This is another reason it is recommended you use the Jupyter hub attached to the SAS Viya environment to make SWAT connections.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning and boost your career prospects.