sessionProp

addFmtLib

Description

Adds a format library to the current CAS session. This allows user-defined formats to be used in analyses and data manipulation within CAS. Format libraries can be loaded from a caslib (as a SASHDAT file) or directly from a server path. This action is crucial for customizing data representation and reporting in Viya.

sessionProp.addFmtLib <result=results> <status=rc> / caslib="string", fmtLibName="string", fmtSearch="APPEND" | "INSERT" | "NONE" | "REPLACE", name="table-name", path="string", promote=TRUE | FALSE, replace=TRUE | FALSE;
Settings
ParameterDescription
caslibSpecifies the caslib where the format library resides. Do not specify this parameter with the path parameter.
fmtLibNameSpecifies the format library name. The name cannot exceed 63 characters in length.
fmtSearchSpecifies the format library search order. Can be APPEND, INSERT, NONE, or REPLACE. Default is APPEND.
nameSpecifies the format library name in the caslib. If adding from a SASHDAT file, the .sashdat extension is optional. Do not use with the path parameter.
pathSpecifies the path to a format library file. The path must be readable from the server's control node. Do not use with the name or caslib parameter.
promoteWhen set to True, the format library is promoted to global scope, making it available to all sessions. Administrative rights might be required.
replaceWhen set to True, an existing format library of the same name is replaced.
Data Preparation View data prep sheet
Creating a Control Data Set for a Format Library

Before adding a format library to CAS, you first need to create one. A common method is to use PROC FORMAT with the CNTLOUT= option. This creates a SAS data set that contains the definition of your formats. This data set can then be saved as a SASHDAT file and loaded into CAS.

Copied!
1PROC FORMAT;
2 value $genderfmt 'M'='Male' 'F'='Female';
3 value agefmt 1-12='Child' 13-19='Teen' 20-64='Adult' 65-high='Senior';
4RUN;
5 
6/* Create a control data set from the formats */
7LIBNAME mycas cas;
8DATA mycas.myformats_table;
9 SET sashelp.vformat;
10 where fmtname in ('GENDERFMT', 'AGEFMT');
11RUN;

Examples

This example assumes a CAS table named 'myformats_table' exists in the 'mycas' caslib (created in the data creation step). It adds this table as a format library named 'myformats' to the current session.

SAS® / CAS Code Code awaiting community validation
Copied!
1 
2PROC CAS;
3sessionProp.addFmtLib / caslib="mycas" name="myformats_table" fmtLibName="myformats";
4 
5RUN;
6 
Result :
The format library 'myformats' is successfully added to the session. A confirmation note will appear in the log, and the formats within it (like $GENDERFMT and AGEFMT) become available for use in CAS procedures.

This example demonstrates a complete workflow. First, it creates a format library and saves it as a SASHDAT file to a physical path accessible by the CAS server. Then, it uses `addFmtLib` with the `path` parameter to add it to the session. The `promote=true` option makes it globally available, and `replace=true` ensures any existing library with the same name is overwritten.

SAS® / CAS Code Code awaiting community validation
Copied!
1LIBNAME mycas cas;
2PROC FORMAT lib=work;
3 value $countryfmt 'US'='United States' 'CA'='Canada';
4RUN;
5PROC FORMAT lib=work cntlout=work.countryctl;
6 select $countryfmt;
7RUN;
8DATA mycas.countryformats;
9 SET work.countryctl;
10RUN;
11PROC CASUTIL;
12 save casdata="countryformats" incaslib="mycas" outpath="/cas/data/casuser/countryformats.sashdat" replace;
13QUIT;
14PROC CAS;
15 sessionProp.addFmtLib /
16 path="/cas/data/casuser/countryformats.sashdat"
17 fmtLibName="globalformats"
18 promote=true
19 replace=true;
20RUN;
Result :
The format library 'globalformats' is added to the session with global scope, replacing any pre-existing library of the same name. The log will show notes confirming the successful saving of the SASHDAT file and the addition of the format library. The formats are now available across all CAS sessions.

FAQ

What is the purpose of the addFmtLib action?
How do I specify the location of the format library using the 'caslib' and 'name' parameters?
What is the 'fmtLibName' parameter used for?
How can I specify the format library's location using a direct file path?
What does the 'replace' parameter do?
How does the 'promote' parameter affect the format library's scope?
What are the options for the 'fmtSearch' parameter?

Associated Scenarios

Use Case
Standard Workflow: Customer Segmentation for Marketing Campaign

The marketing department needs to analyze campaign outreach results. They require custom formats to segment customers by age group and geographical region, which are not standar...

Use Case
Global Scope & Replacement: Centralized Financial Transaction Risk Formats

A financial institution maintains a centrally managed, large set of formats for categorizing transaction risk. This library must be available to all data scientists and analysts...

Use Case
Edge Case: Resolving Format Name Conflicts with fmtSearch

Two departments, Marketing and Fraud, have independently developed format libraries. Both libraries contain a format with the exact same name ('$STATUSFMT') but with different b...