Connect Viya to VS Code for SAS Development Productivity

Kris Matson
7 min readOct 9, 2023
Photo by ian dooley on Unsplash

The SAS extension for VS Code is a productivity game-changer for Data Scientists developing in SAS, SQL, and Python, locally and in the cloud. The extension easily connects to SAS cloud computing environments, letting you run SAS, SQL, and Python in VS Code, just as if you were working on Viya in SAS Studio on the browser.

As a machine learning and artificial intelligence systems developer, I have long been using VS Code to write Python, SAS, SQL, and C++ code locally. Recently, I discovered how easy it is to connect VS Code to SAS Viya cloud instances and write and execute code locally without logging into the Viya browser interface. There are only a few quick steps to install, connect, and use the SAS VS Code extension for this huge development productivity boost!

Get it

Select the “extensions” icon in the VS Code Activity Bar and search for “SAS”. Locate the extension labeled “Official SAS Language Extension for VS Code”, with the blue checkmark next to “SAS Institute Inc”. Click the “install” button to load the extension. (In the image below I have already installed the extension so the install button does not appear.)

Connect it

After the SAS extension is loaded, you need to connect to your Viya cloud servers for the first time.

  1. The fastest way to do this is to select the SAS logo appearing in the Activity Bar, and click the “Sign In” button. This brings up a prompt to “Add new connection profile”. You can also bring up the Command Palette (Cmd-Shift-P or Ctrl-Shift-P), start typing “SAS..”, then select “SAS: Add new connection profile”
  2. Select the connection type from the list. Note that you can connect SAS Viya, a SAS 9 Remote server, or a local SAS 9 COM server. These instructions assume you are connecting to SAS Viya.
  3. Next, enter the base URL to the Viya server for this connection. The base URL is the root domain for your Viya instance with no additional paths or routes, such as https://viya1.sas.com/.
  4. Press “enter” to select the default in the next box for “SAS Job Execution compute context”. VS Code uses Viya job execution API hooks behind the scenes to run your commands and programs from VS Code.
  5. In the “client ID” field, leave this blank and simply press enter if you are connecting to a Viya version after 2023.01. If you are connecting to version 2022.12 or before, you will need to obtain a “client id” from your Viya administrator.
  6. Next, VS Code will ask if it can “open the external website” to sign in. Check that the correct Viya server URL displays in the dialog and press continue. This will open a browser with the login screen for your requested Viya instance, and then return an authorization code after you enter your credentials.
  7. Copy the authorization code from the browser and paste it into the final text entry box in VS Code and press enter.
  8. When the connection is successful, you will see a SAS Explorer pane with your folders, files, and libraries just as they appear on your remote Viya instance! Your profile name will appear in the VS Code status bar on the lower left.

The steps above conveniently store your SAS connection profile(s) in your VS Code user/setting.json file. Now when you start VS Code, you can connect to a SAS server in your profile and get to work right away! To switch the SAS environment you are connected to, simply select the profile icon in the Status Bar to bring up a selection of your server instances in the Command Palette.

Let’s Get to Work

Several development workflow actions will help you get to work quickly in Viya from VS Code.

First, select the SAS icon in the Activity Bar on the left and you will see the SAS explorer panes in VS Code just as they appear in SAS Studio in the browser. You can navigate your familiar hierarchy of cloud-based files, folders, and libraries to display code and work with them in VS Code. You can also navigate your data libraries and select datasets and databases. To view the data contents of any dataset or database table, just double-click on the dataset or table name.

Next, bring up the Command Palette again and type “sas:”. This brings up all the SAS-related options. You can Update, Delete, or Switch your SAS server connection profiles or add a new one. Other commands let you run SAS code and manage your session.

Running Code

You can run SAS, SQL, and Python code from both your local file system as well as from your online, cloud-based files. Select the SAS icon in the Activity Bar and then double-click a file from the cloud file explorer. The file will be displayed for editing and execution.

Right-click “New File” on a cloud directory path to create a new code file for editing. Enter some simple SAS code to generate a histogram of data from the SASHELP.CARS dataset:

proc univariate data=sashelp.cars noprint;
histogram Horsepower;
run;

Next click the run icon in the Tab Bar. Your code will execute and display the PROC UNIVARIATE results in the VS Code results pane by default. (If the sashelp library is not installed on your system, run this code against one of your own datasets). Because ODS HTML5 graphics are enabled by default, you will see the procedure results in a new “Result” tab. The corresponding SAS Log printout appears in the “OUPUT” pane.

Results displayed in VS Code from running a server-side, cloud-based SAS code file.

Now create a new local sas code file to run against the cloud server. Select the VS Code Explorer icon in the upper left of the VS Code Activity Bar. Next, select the local folder where you would like to create the new file and right-click “New File…”. Name the file with a “*.sas” suffix to activate the VS Code SAS extension as shown in the screenshot below.

Now enter another PROC UNIVARIATE statement to generate a histogram of the SASHELP.CARS Weight variable distribution, with a printout of the first 10 records in the dataset:

proc univariate data=sashelp.cars noprint;
histogram Weight;
title "Cars - Weight";
run;

proc print data=sashelp.cars(obs=10);
run;

Click the “run” button to execute the code. The extension submits your local code to the Viya server, then returns the results to VS Code when execution completes. The results are displayed as before but this time they have been generated from local code! To see the dataset printout, simply page down further in the new “Results” tab.

Results displayed in VS Code from running a local SAS code file.

SAS Notebooks

One of my favorite options in the SAS VS Code Extension are SAS Notebooks. SAS Notebooks work just like Python notebooks in JupyterLab.

From the Command Palette, select “SAS Notebook: New SAS Notebook”. This brings up a new SAS Notebook file (*.sasnb). You can press the “+ Code” or “+ Markdown” buttons to add new cells. Write and run your SAS, SQL, or Python code in the code cells, with annotations in the markdown cells. I like prototyping Python code in Jupyter this way, and now I can do the same thing with SAS code!

The first SAS Notebook example below shows the execution results from running SQL in a code cell. Click the language identifier in the lower right of the cell to select the desired code interpreter, or simply let the notebook auto-detect the language. Here I have entered ANSI SQL code to return a portion of the SASHELP.CARS dataset, displaying the data in the notebook output window.

Enter the following code in a SQL cell and click the run (play, execute) button on the left side of the notebook cell.

SELECT Make, Model, Origin, DriveTrain, MSRP, Invoice, Cylinders, Horsepower FROM SASHELP.CARS
WHERE Horsepower GE 350
ORDER BY Make, Horsepower;
Results generated in VS Code from running a SQL statement in a SAS Notebook.

The second SAS Notebook example demonstrates a statistical procedure to evaluate the correlation between vehicle engine size, horsepower, and weight in the SASHELP.CARS dataset. Enter the code below to get the same results.

ods graphics on;
proc corr data=sashelp.cars
pearson
plots=matrix(histogram);
var EngineSize
Horsepower
Weight;
run;

Pressing the run button generates the statistics and the plot in the output images below. Note that to generate the graphics, it is necessary to turn on ODS graphics with: ods graphics on; .

Paging down further in the notebook output window shows the expected multivariate scatter plot display generated by PROC CORR:

It’s a Wrap

With these instructions and some SAS code to give you a head-start, you will be up and running on SAS in VS Code in just a few minutes!

Let me know if you find the SAS extension as useful as I do, I would love to hear your thoughts.

Click here to subscribe to my posts.

--

--

Kris Matson

Software development and research in Machine Learning, Artificial Intelligence, and Data Science.