BookmarkSubscribeRSS Feed

Making SAS programs stored on the file system available to SAS Viya folders

Started ‎01-09-2019 by
Modified ‎08-26-2021 by
Views 7,686

In SAS Viya 3.4, there are two versions of SAS Studio. SAS Studio 5.1 is the new microservices-based version with a new interface. It integrates with other SAS Viya components (SAS Drive, Launcher & Compute) and is available in full deployments. Edoardo Riva wrote an article covering some of the architectural differences between the two SAS Studios. The different architecture of SAS Studio 5.1 means that it also has some functional differences from the non-micro service based versions of SAS Studio. One of the major differences is that SAS Studio 5.1 cannot open and save programs from the the OS file system.


This is a big change for existing SAS Studio users who are used to saving and opening their SAS code from directories on the file system. SAS Studio 5.1 instead allows users to load and save SAS programs to and from SAS Viya folders. The programs themselves are stored in the SAS Viya infrastructure data server using the file service.


For users who have a lot of SAS programs stored on the file system, this will initially be inconvenient. Fortunately, there is a fairly easy way to copy programs (or any other files for that matter) from the OS file system to a SAS Viya folder and vice-versa.


The filename statement includes the new file service (FILESRVC) access method that enables SAS programs to store and retrieve content using the SAS Viya Files service. The SAS Viya files service provides persistence of files, such as SAS programs, logs comment attachments, report images, etc.


There are two methods available to access files. You can access files directly using the file uniform resource identifier (URI). The URI is how SAS Viya uniquely identifies resources. You can also access files using the collection option, which is very useful when your files are attached to folders. The collection option means FILSERVC access method can both store files and associate files to folders.


So how do we use this filename to copy a file from a directory on the filesystem to a SAS Viya folder? The SAS program below uses:

  • filename statement to access files in a LINUX directory
  • a second filename statement with the files service access method to access a SAS Viya folder
  • the fcopy function to copy the files between the two locations.
/* filename to a sas program on the file system */

filename src "~/";

/* filename using FILESRVC to the destination a sas program and folder */

filename dest filesrvc folderpath="/Public" filename="" debug=http;

/* copy the file: output return code and any message */
data _null_;
put rc=;
put msg=;


Simple as that! When you run the program, the file is copied from the home directory of the user on Linux to the Public folder in SAS Viya. SAS Studio 5 users can now open, edit, and run the program from its location with a SAS Viya folder.


The debug=http option on the FILESRVC filename statement is useful when you have problems, and to see what is happening behind the scenes. The extract from the log below shows the end result: a file created in the file service and referenced within the folder requested.   



Click any image to see a larger version.


The SAS program is now in the file service and can be surface in SAS Studio 5 or SAS Drive.




By reversing the source and destination it is also possible to go the other way and copy a file from SAS Viya to the operating system.


The logical extension of this approach is to copy a complete directory of SAS programs from the operating system to a folder in SAS Viya. The attachment below contains a SAS macro that will copy all the SAS programs in a OS directory to a folder in SAS Viya. It basically loops the directory content and submits the sample code shown above.


This all works well from SAS Studio 5.1 on SAS Viya, which is completely integrated with the SAS Viya micro-services and authentication. There are a couple of additional steps required to access the file service from SAS 9.4 M6 or earlier versions of SAS Studio.


Firstly, the servicesbaseurl SAS option must be set. The servicesbaseurl option tells the SAS session the root URL for calls to SAS Viya services. Servicesbaseurl is an invocation option, meaning it can only be set when SAS is starting. To set the servicesbaseurl, modify the SAS configuration file of your SAS session and add the following line, referring to your SAS Viya server:




In addition, when using anything other than SAS Studio 5.1, you must have authenticated to SAS Viya and provided your session access to the token by setting the SAS_VIYA_TOKEN environment variable. The SAS_VIYA_TOKEN environment variable can be set at invocation time, or in a options statement within your SAS code. For example:


option set SAS_VIYA_TOKEN=<your token>


There are a number of approaches you can use to get the authentication token in your SAS session. The attachment includes two macros:

  • %authtoviya authenticates to SAS Viya using proc http and sets the SAS_VIYA_TOKEN environment variable.
  • %getaccesstoken assumes you have authenticated using the sas-admin CLI and reads the access token to set the SAS_VIYA_TOKEN environment variable.

The transition to using SAS Viya folders instead of operating system directories can be made easier using the file service access method of the filename statement. In addition, the access method can be used to read and write may other file types to and from a SAS Viya environment.


I hope you find this useful. Here are some additional resources:


That's a great solution!


Do you have any advice on how to pass the user password to PROC HTTP's IN option?

Thanks! Take a look at the $authtoviya macro in the attachment to the article It shows how to connect with a userid and password.

Version history
Last update:
‎08-26-2021 04:29 PM
Updated by:



Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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