BookmarkSubscribeRSS Feed
ChrisHemedinger
Community Manager

In SAS Viya Workbench, you can use a SAS Notebook to organize your program code with documentation and output, just as you might have done with Jupyter or Python notebooks. The SAS Notebook file type is supported in the SAS extension for VS Code, which is baked into SAS Viya Workbench. Personally, I love to use the SAS Notebook format for step-by-step programming demos; it's a great way to organize steps and explanations.

 

Sample SAS Notebook for a demoSample SAS Notebook for a demo

 

Some users have ask for a way to %INCLUDE a SAS Notebook file into a SAS session to run the code that you've stored in that external file. %INCLUDE is a tried-and-true method to allow users to organize code libraries -- collections of .SAS files -- for easy re-use, but the SAS runtime cannot read SAS Notebook files (.sasnb files) directly.

 

I've created a SAS macro called %includesasnb that supports this task. The macro takes advantage of the fact that notebook files are structured using JSON, and SAS can easily read JSON with the JSON libname engine. SAS Notebooks support SAS, SQL and Python code cells, and the macro will assemble all of this content from a SAS Notebook into a continuous program to run in your session. You can also optionally output the full SAS program to another file or to the SAS log.

 

Using %includesasnb in SAS Viya WorkbenchUsing %includesasnb in SAS Viya Workbench

 

You can find the %includesasnb macro and brief documentation about it in this GitHub location. If you want to pull the macro directly into your SAS Viya Workbench session from GitHub, this code segment does that for you:

 

/* Pull %includesasnb.sas from GitHub */
filename src "%sysfunc(getoption(WORK))/includesasnb.sas";
 
proc http
 method='GET'
 url="https://raw.githubusercontent.com/sascommunities/sas-dummy-blog/master/includesasnb/includesasnb.sas"
 out=src;
run;
 
%include src;

 

You can then use the macro to run code from a SAS Notebook you've stored in your Workbench:

 

/* Use the macro to run my local notebook file */

%includesasnb(filepath=sasnotebooks/sample-nb.sasnb);
SAS Innovate 2025: Call for Content! Submit your proposals before Sept 25. Accepted presenters get amazing perks to attend the conference!