SAS Notebooks in VS Code: A developer’s guide

0

You're probably familiar with Jupyter Notebooks. The ability to combine code cells with their logs and outputs, along with Markdown cells for easy documentation, makes them incredibly useful.

When you start up SAS Viya Workbench and select Visual Studio Code as your preferred editor, you can use a powerful new tool: SAS Notebooks.

SAS Notebooks are interactive notebooks for SAS coders that include the following three cell types:

  1. Markdown: Write your Markdown, click Run, and the Markdown will render.
  2. SAS: Write SAS code with full syntax highlighting, typing hints, and autocompletion.
  3. SQL: Write your Proc SQL code without needing the surrounding PROC wrapper, allowing you to focus on the essentials.

Creating a new SAS Notebook

Okay, let's look at how we can create a new SAS Notebook. Simply right-click on Explorer and give your notebook a name. Then use the file extension .sasnb - e.g., example.sasnb.

When you open the SAS Notebook, it should look like this, depending of course on your selected theme:

  1. Click on the Code button to add a code cell to your notebook.
  2. Click on the Markdown button to add a Markdown cell to your notebook.
  3. The Run All button enables you to run all cells within the notebook.
  4. The Clear All Outputs button enables you to reset the output of all cells.
  5. This little overflow menu hides additional customization options for the look and feel of your notebooks and provides the ability to export the notebook as a .sas or .html file. There is one option called Notebook Line Numbers which I have activated for further screenshots.

Let's explore

Let's take a more detailed look at the different cell types to give you a taste of what is possible.

First up is the Markdown cell. Just write using Markdown syntax, and you'll immediately see syntax highlighting:

  1. You can run an individual cell by clicking the Run button or by using the keyboard shortcut Ctrl + Enter.

Running the Markdown cell renders it in place. To return to editing mode, just double-click the cell:

Next up is the SAS code cell:

  1. Hovering over a SAS keyword brings up the help functions.
  2. When you run a cell, you get a checkmark and the total runtime of your code.
  3. And beneath the cell, you can see the result or the SAS log.

Now let's create a SQL code cell. To do that, add a new code cell as before. You'll notice that the bottom right corner still says SAS. If you click on it and the dialog shows MS SQL, don't worry—it's still running Proc SQL. This is a known behavior in VS Code itself, not an issue with SAS:

  1. Remember, while it says MS SQL, it means SQL or even better Proc SQL. All the great Data Step functions you are used to having are available.
  2. As with the SAS code cell, we get a checkmark and a runtime indicator.
  3. And finally, we see the result of our query right below the cell itself.

Conclusion

SAS Notebooks are an awesome way to program in SAS and combine it with documentation, making it easier to iterate and boost your productivity. Using the export functionality mentioned at the beginning of the article, you can turn your notebook into SAS code files to easily share with others, or export it as an HTML file so everyone can see the great results you produced.

Learn more

Share

About Author

David Weik

Sr Software Development Engineer in Test

For over five years I have had the opportunity to dive deep into discussions about Code of Conducts, regulations like the GDPR and how these influence data warehouses and analytic projects. By working closely with practitioners across departments like business value, actuary, IT operations and data protection officers we were always able to arrive at solutions that balance the interests of these diverse stakeholders. Utilizing a strong platform with build in governance, relying on white-box models and open discussions we can achieve solutions that create both business value and give confidence that a fair decision was reached.

Leave A Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Top