Using Python to work with SAS Viya and CAS


One of the big benefits of SAS Viya is how approachable it is for programmers of other languages. You don't have to learn SAS in order to become productive quickly. We've seen a lot of interest from people who code in Python, maybe because that language has become known for its application in machine learning. SAS has a new product called SAS Visual Data Mining and Machine Learning. And these days, you can't offer such a product without also offering something special to those Python enthusiasts.

Update: SAS now offers a free trial for developers -- scroll down here for more details!

Introducing Python SWAT

And so, SAS has published the Python SWAT project (where "SWAT" stands for the SAS scripting wrapper for analytical transfer. The project is a Python code library that SAS released using an open source model. That means that you can download it for free, make changes locally, and even contribute those changes back to the community (as some developers have already done!). You'll find it at

SAS developer Kevin Smith is the main contributor on Python SWAT, and he's a big fan of Python. He's also an expert in SAS and in many programming languages. If you're a SAS user, you probably run Kevin's code every day; he was an original developer on the SAS Output Delivery System (ODS). Now he's a member of the cloud analytics team in SAS R&D. (He's also the author of more than a few conference papers and SAS books.)

Kevin enjoys the dynamic, fluid style that a scripting language like Python affords - versus the more formal "code-compile-build-execute" model of a compiled language. Watch this video (about 14 minutes) in which Kevin talks about what he likes in Python, and shows off how Python SWAT can drive SAS' machine learning capabilities.

New -- but familiar -- syntax for Python coders

The analytics engine behind SAS Viya is called CAS, or SAS Cloud Analytic Services. You'll want to learn that term, because "CAS" is used throughout the SAS documentation and APIs. And while CAS might be new to you, the Python approach to CAS should feel very familiar for users of Python libraries, especially users of pandas, the Python Data Analysis Library.

CAS and SAS' Python SWAT extends these concepts to provide intuitive, high-performance analytics from SAS Viya in your favorite Python environment, whether that's a Jupyter notebook or a simple console. Watch the video to see Kevin's demo and discussion about how to get started. You'll learn:

  • How to connect your Python session to the CAS server
  • How to upload data from your client to the CAS server
  • How SWAT extends the concept of the DataFrame API in pandas to leverage CAS capabilities
  • How to coax CAS to provide descriptive statistics about your data, and then go beyond what's built into the traditional DataFrame methods.

Try it -- for free! -- in your browser

Want to get your hands on this? With just a couple of clicks, you can sign into the SAS Viya Developer Free Trial. You sign in with your profile (the same one you use in SAS Support Communities or to access SAS e-learning). The trial environment lets you test-drive SAS Viya using Python or R in a Jupyter notebook, or with SAS programming using SAS Studio. When you sign up and click through to the trial, you'll have your own session that you can use for the next 8 hours -- plenty of time to kick the tires.

The trial environment includes all of the sample data and code that you need to get a feel for what it's like to code with CAS. You can find all of the API reference documentation at the SAS for Developers site. SAS developer Jared Dean narrates this great demo/tutorial that you can watch or follow along with during your experience.

Learn more about SAS Viya and Python

There are plenty of helpful resources to help you learn about using Python with SAS Viya:

And finally, what if you don't have SAS Viya yet, but you're interested in using Python with SAS 9.4? Check out the SASPy project, which allows you to access your traditional SAS features from a Jupyter notebook or Python console. It's another popular open source project from SAS R&D.


About Author

Chris Hemedinger

Director, SAS User Engagement

+Chris Hemedinger is the Director of SAS User Engagement, which includes our SAS Communities and SAS User Groups. Since 1993, Chris has worked for SAS as an author, a software developer, an R&D manager and a consultant. Inexplicably, Chris is still coasting on the limited fame he earned as an author of SAS For Dummies


  1. Hello,

    I am new to SAS Viya and we are planning to use it as a deployment tool in our company in the US. I wanted to know as an R and Python programmer, am I free to use whatever packages that I want when I am modeling with R and Python and then build the model and read it into SAS Viya for just the deployment or I am limited to use CAS package functions?

  2. I am running python on a windows machine. Will I be able to using any python to access SAS Viya? Is there a specific install required on the SAS server to enable this functionality?

    • Chris Hemedinger
      Chris Hemedinger on

      Nothing special needed beyond the Python SWAT package and its dependencies. From Windows, you'll be using the HTTP protocol to access SAS Viya (binary protocol isn't yet supported). The GitHub project instructions should help, and you might read through the issue conversations that others have contributed.

  3. Can we use other Python IDE like Pycharm , Apache Zeppelin, Rstudio, Rodeo IDE and Spark Notebook to use SWAT instead of jupyter notebok . What are challenges if we cant' use them ?

  4. Hi Chris,

    Thanks for your reply .

    1-We have many CAS actions and actions sets mentioned here -

    But when we do then we able to see very few CAS actions and action set that we can use.

    So it's under development for other cas actions or it is not planned or not feasible to call other actions using SWAT.

    when we do conn. then it shows all available cas actions along with some other methods which are not cas actions like read_csv which are based on python /pandas packages.

    There are functions like concat which are under swat.functions which we can't call using conn. i.e. session object.
    But there are object like swat.cas.table.CASTable which we can call using session object i.e. conn.CASTable().

    Also we have 2 options i.e. swat.cas and swat.CAS and both have different methods , so what's difference in that?

    So my questions is why is it so where few methods/functions we can call using session reference and some are not ? Is there a document where I can find the basics of it or the flow of the swat package ?

    • Hi Anant,
      To answer your first question, the SWAT package has a handful of action sets at install. You can see the action sets by running

      Here are some helpful commands for displaying action sets and actions:
      Put the available action sets in a variable:
      out =
      Give me a list of action sets- returns results in a dictionary:
      List the action sets in table form:
      List a specific action set:

      If you want to use actions from other action sets, you import them first:
      Now you can use any actions in the aggregation action set. Here is a link to all actions and action sets.

      The SWAT documentation is found at this link. You may want to refer to the API reference section for more info on swat.CAS. Another good place to start would be on the's page on Python.

  5. Pingback: Should you be banking on open source analytics? - Hidden Insights

Back to Top