BookmarkSubscribeRSS Feed

Snippets: How to make a repository of Snippets available to all SAS Studio users

Started ‎09-10-2024 by
Modified ‎09-13-2024 by
Views 1,701

SAS Studio provides a common set of snippets. Snippets are lines of commonly used code or text that you can save and reuse. Currently there are two areas for snippets: "SAS Snippets" (provided by SAS) and "My Snippets" (the ones you create yourself).

 

You might want to have a third area "My Company Snippets". These would be user created snippets available to all SAS Studio users. This is currently not directly supported. This post will show you a way how to achieve this.

 

This method uses the functionality to create a shortcut to a SAS Content folder in another folder. This can be done manually using the "Share and Collaborate" application. In this post we are going to use the Folders API to do it, so it can be automated.

 

The basic steps are:

 

  1. Create a folder that will hold general snippets
  2. Create a shortcut to this folder in each users "My Snippets" folder
  3. Add snippets to the folder from step 1

 

Snippets basics

 

When you create a snippet the screen will look like this:

 

01_bm-snippets-screen.png

Select any image to see a larger version.
Mobile users: To view the images, select the "Full" version at the bottom of the page.

 

We have the following elements:

 

  • Name: the name we will see in the Snippets pane
  • Abbreviation: This is a shortname we can give to the snippet. In the SAS program editor you can enter @abbreviation to add the text for a snippet or just enter @ to get a list of abbreviations.
  • Description: some text explaining the purpose of the snippet
  • Folder: this is the SAS content folder location where we will store the snippet. All the folders below the "My Snippets" folder are accessible
  • Text: the text associated with the snippet

 

User defined snippets are stored as .csm files in a SAS content folder. See Working with Snippets for more information on using snippets. Now let's look at the three basic steps.

 

Step 1: Create a folder that will hold general snippets

 

In our example we create a folder for snippets in the Public folder. By default all users have read and write permission in this folder. Of course you may choose another folder where only certain users will have write access.

 

We use the sas-viya CLI like so:

 

sas-viya folders create --parent-path /Public --name "Company-Snippets" --description "Company provided snippets"

Of course the folder can be created by any other method available like using the Explorer in SAS Studio.

 

Step 2: Create a shortcut to this folder in each users "My Snippets" folder

 

Now we need to create a shortcut to this new folder in each users "My Snippets" folder. This allows for easy access and the use of the @abbreviation functionality.  This can be done manually using the "Share and Collaborate" (SASDrive) application. For an automated implementation we are using operations from the Folders API to do this.

 

To add a shortcut to a folder in another folder we use the Add a member to a folder operation. This operation requires two folder URI's. Now you might ask what is a folder URI. You find more details in this article Uniform Resource Identifiers (URI) in SAS Viya, specifically Content URI's.

 

To get the folder URI from a SAS content folder we will make use of the FILESRVC file access method as it will create a macro variable with the folder URI. For more details see Generating a FILESRVC Macro Variable. See this code sample:

 

%symdel FILESRVC_SNIPFLD_URI / nowarn;
filename snipfld filesrvc folderpath="/Public/Company-Snippets";
%put NOTE: &=_FILESRVC_SNIPFLD_URI;
%symdel _FILESRVC_MYSNIP_URI / nowarn;
filename mysnip filesrvc folderpath="/Users/&sysuserid/My Folder/My Snippets";
%put NOTE: &=_FILESRVC_MYSNIP_URI;

 

An alternative approach would be the Get a folder with a path or a child URI operation. The following code example shows how to do this.

 

%let base_url = %sysfunc(getoption(servicesbaseurl));
%put NOTE: &=base_url;

%let folderName = /Public/Company-Snippets;

filename resp temp;
proc http
method=get
url="&base_url/folders/folders/@item"
out=resp
query=(
"path" = "&folderName"
)
oauth_bearer=sas_services
;
headers
"Accept" = "application/vnd.sas.content.folder+json, application/vnd.sas.content.folder.member+json"
;

run;
%put NOTE &=SYS_PROCHTTP_STATUS_CODE;
%put NOTE: response;
%put %sysfunc(jsonpp(resp, log));

libname resp json noalldata;
%symdel folderUri / nowarn;
data _null_;
set resp.links;
where rel = "self";
call symputx("folderUri", uri);
run;

%put NOTE: &=folderUri;

 

Next we use the Add a member to a folder operation to add the shortcut to the "My Snippets" folder. In the code sample below the following macro variables are used:

 

  • &folderWithSnippets holds the folder URI of the folder where we want to store the snippets.
  • &mySnippetsFolder holds the folder URI of the "My Snippets" folder

 

The "type": "reference" in the request body indicates that we want to create a shortcut.

 

%let base_url = %sysfunc(getoption(servicesbaseurl));
%put NOTE: &=base_url;
/*
 * create request body
 */
filename req temp;
data _null_;
  infile cards;
  input;
  _infile_ = resolve(_infile_);
  file req;
  put _infile_;
cards;
{
  "name": "Company-Snippets",
  "type": "reference",
  "uri": "&folderWithSnippets",
  "contentType": "folder"
}
;
filename resp temp;
proc http
  method=post
  url="&base_url/&mySnippetsFolder/members"
  in=req
  out=resp
  oauth_bearer=sas_services
;
  headers
    "Accept" = "application/vnd.sas.content.folder.member+json,application/vnd.sas.error+json"
    "Content-Type" = "application/json"
   ;
run;
%put NOTE &=SYS_PROCHTTP_STATUS_CODE;
%put NOTE: response;
%put %sysfunc(jsonpp(resp, log));

 

A value of 201 in the macro variable SYS_PROCHTTP_STATUS_CODE indicates success.

 

You might want to add this code to a SAS macro, so that users have an easy way of running it.

 

Step 3: Add snippets to the folder

 

Let's create the snippets we need in the "Company-Snippets" folder.

 

If you already have a collection of snippets in your "My Snippets" area, use the Explorer pane and copy the corresponding snippet files (.csm) from the "My Folder/My Snippets" to the proper folder.

 

If you create a new snippet choose the right folder to store it.

 

02_bm-snippets-screen-choose-folder.png

 

The snippet is now available for use in the SAS program editor

 

03_bm-use-snippet.png

 

The use of @abbreviation is switched on by default, but it can be switch off, see Setting Editors Preferences for more details.

 

To group your snippets, make use of sub folders within the "Company-Snippets" folder.

 

Summary

 

It is possible to add a folder that contains snippets, files with .csm extension, to the "My Snippets" in the Snippets section of the navigation pane. This has to be done individually either using the SASDrive application or using code as shown. The advantage of having snippets available through "My Snippets" is the use of abbreviations assigned to a snippet.

 

 

Find more articles from SAS Global Enablement and Learning here.

Version history
Last update:
‎09-13-2024 02:34 PM
Updated by:
Contributors

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.

Register now!

Free course: Data Literacy Essentials

Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning  and boost your career prospects.

Get Started

Article Tags