BookmarkSubscribeRSS Feed

Accessing SAS Content with FILENAME FILESRVC in SAS Viya

Started ‎07-14-2025 by
Modified ‎07-14-2025 by
Views 1,695

In SAS. the FILENAME statement is a versatile mechanism to read and write file content...even when that content isn't stored in a traditional file system. SAS Viya uses SAS Content folders as a streamlined mechanism to manage and access files, independent of a mounted file system. To access this content in your SAS programs, SAS provides the FILENAME FILESRVC method.

 

Review: Using the FILENAME statement in SAS

SAS uses the FILENAME statement to define references (called filerefs) to external or internal file locations—such as local files, URLs, or cloud-based content repositories.​ A fileref can point to a specific file or (for some methods) a collection of files (like a folder).​ This allows programmers to separate knowledge of the file path syntax (specific for each OS or resource type) from the code that has to reference it.

 

For example, this DATA step reads a text file using the fileref myfilemyfile can be defined using any of the supported FILENAME methods, and it doesn't affect how the DATA step works. 

 

data input;
 infile myfile;
 input x y z;
run;

/* any of these FILENAME definitions can work for this data step */
filename myfile "c:\projects\mydata.csv";
filename myfile "/u/myaccount/projects/mydata.csv";
filename myfile ZIP "c:\projects\archive.zip" member="mydata.csv";
filename myfile URL "https://mysite.com/data/mydata.csv";

 

 Using FILENAME member syntax

When a fileref points to a collection (like a folder), SAS allows you to reference individual files using member syntax. This is done by appending the file name in parentheses after the fileref. For example:

filename myfolder '/path/to/folder';
data _null_;
  infile myfolder('example.csv');
  input;
  put _infile_;
run;

This syntax is especially useful when working with directory-style file sources, such as disk folders, ZIP files, and SAS Content;  multiple files may be stored and accessed programmatically using a single FILENAME assignment.

 

Understanding SAS Content Folders

SAS Content folders are more than just storage—they are a central part of the SAS Viya ecosystem. These folders can contain:

  • Data definitions
  • SAS programs and jobs
  • Visual Analytics reports
  • Traditional files like CSVs or Excel spreadsheets

They are governed by role-based permissions and are integrated into the SAS Viya platform’s backup and migration processes. However, unlike traditional environments, mapped file systems may not always be available in Viya, making native access methods like FILESRVC essential. In cloud-hosted Viya environments, FILESRVC abstracts away the underlying file system, making it easier to deploy and manage code across environments.

 

Introducing the FILESRVC access method for FILENAME

The FILESRVC method stands for “SAS Viya Files Service.” It is a new FILENAME access method designed specifically for SAS Viya. With FILESRVC, users can directly access files stored in SAS Content folders using a simple and secure syntax.

 

Here’s a basic example:

filename myfile filesrvc folderpath='/Users/yourname/MyFolder' filename='data.csv';
data _null_;
  infile myfile;
  input;
  put _infile_;
run;

 

You can also use FILESRVC in an %INCLUDE statement to access and run a SAS program. This example assigns a fileref to the "Dummies" folder the My Folders area, and then runs a SAS program named "bird-events.sas". Note that in this example, the .SAS file extension is implied. The SOURCE2 system option in SAS controls whether the source code of included files is displayed in the SAS log. When SOURCE2 is active, the log will show the statements from the included file, along with the fileref and filename, and the nesting level of the included file.

filename birds filesrvc
 folderpath="/Users/&sysuserid./My Folder/Dummies"
 ;

%include birds(bird-events) / source2;

 Partial log output:

80    filename birds filesrvc
81     folderpath="/Users/&sysuserid./My Folder/Dummies"
82     ;
83    
84    %include birds(bird-events) / source2;
NOTE: %INCLUDE (level 1) file BIRDS(bird-events) is file bird-events.sas.
85   +/*
86   +   This script retrieves bird detection data from BirdNET-Pi and generates a heatmap of bird detections by day.
87   +   The data is sourced from a CSV file hosted on GitHub.
88   +*/

 

Copy a file to SAS Content using FCOPY function

With fully qualified fileref definitions, you can use the FCOPY function in SAS DATA step to copy file content between locations. For example, this example shows how to copy a SAS program from a disk file path to a SAS Content folder, and then use %INCLUDE to run it. 

/* &_USERHOME defined in SAS Studio for file home path */
filename src "&_userhome./Dummies/piechart.sas";
filename tgt filesrvc
 folderpath="/Users/&sysuserid./My Folder/Dummies"
 filename="piechart.sas";

 /* copy file with FCOPY */
data _null_;
 rc=fcopy('src','tgt');
run;

%include tgt;

 

Using SAS functions to get file details from SAS Content files

Use FOPEN, FOPTNUM, FOPTNAME and FINFO to learn the specific SAS Content file properties including URI, Content Type, Size, Created and Modified times, and more. Note that several of these file attributes are unique to SAS Content files.

filename tgt filesrvc
 folderpath="/Users/&sysuserid./My Folder/Dummies"
 filename="piechart.sas";

data deets;
  fId = fopen("tgt","S");
  if fID then
    do;
      infonum=foptnum(fid);
      do i=1 to infonum;
        infoname=foptname(fid,i);
        select (infoname);
          when ('Filename') filename=finfo(fid,infoname);
          when ('File Identifier') fileid=finfo(fid,infoname);
          when ('Content Type') ct=finfo(fid,infoname);          
          when ('URI path') uri=finfo(fid,infoname);
          when ('Encoding') encoding=finfo(fid,infoname);          
          when ('Searchable') searchable=finfo(fid,infoname);          
          when ('File Size (bytes)') filesize=input(finfo(fid,infoname),15.);
          when ('LRECL') lrecl=input(finfo(fid,infoname),15.);
          when ('RECFM') recfm=finfo(fid,infoname);
          when ('Last Modified') modified=input(finfo(fid,infoname),anydtdtm.);
          when ('Create Time') createtime=input(finfo(fid,infoname),anydtdtm.);
        end;
      end;
      output;
    end;
    drop infoname infonum i fid;
    format modified datetime20. createtime datetime20.;
  fId = fClose( fId );
run;
proc print data=deets; run;

Here's a sample output from the program:

ChrisHemedinger_0-1752520896735.png

 

Learn more about FILENAME FILESRVC

See SAS documentation about FILENAME FILESRVC for more background and examples.

Comments

Hi @ChrisHemedinger , this is a useful post, thanks for sharing. Even if this seems tedious or redundant, I recommend coding DISK method explicitly for disk-based Filerefs. The same applies for BASE Librefs. With code completion all around, adding 6 symbols to each Filerefs assignments has become very labor-cheap today. Besides, those kind of normalisations offer some real advantages even if you do not use linting (yet : my case, for instance ) :

  • more code readibility
  • more maintainability as well
  • easier to parse automatically 
  • improving coding assistant LLM accuracy when the normalised code becomes part of the training set

 

The FILESRVC method of the filename statement is a useful beast indeed - you can even use it in pure macro functions!

 

Here are some examples from the SASjs Core library:

 

Contributors
Version history
Last update:
‎07-14-2025 03:26 PM
Updated by:

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

SAS AI and Machine Learning Courses

The rapid growth of AI technologies is driving an AI skills gap and demand for AI talent. Ready to grow your AI literacy? SAS offers free ways to get started for beginners, business leaders, and analytics professionals of all skill levels. Your future self will thank you.

Get started

Article Tags