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:
- Markdown: Write your Markdown, click Run, and the Markdown will render.
- SAS: Write SAS code with full syntax highlighting, typing hints, and autocompletion.
- 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:
- Click on the Code button to add a code cell to your notebook.
- Click on the Markdown button to add a Markdown cell to your notebook.
- The Run All button enables you to run all cells within the notebook.
- The Clear All Outputs button enables you to reset the output of all cells.
- 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:
- 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:
- Hovering over a SAS keyword brings up the help functions.
- When you run a cell, you get a checkmark and the total runtime of your code.
- 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:
- 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.
- As with the SAS code cell, we get a checkmark and a runtime indicator.
- 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
- SAS® Viya® Workbench Demo: Solving Your Productivity Challenges
- Demo: SAS Viya Workbench and SAS code to access Microsoft 365
- Essential VS Code shortcuts every Python developer should know
- Install VS Code extensions offline: step-by-step guide
- Getting started using SASPy® and the SAS® kernel for Jupyter Notebook