sessionProp

addFormat

L'essentiel
At a glance
Raw data often lacks the semantic context required for effective business intelligence. The addFormat command bridges this gap by empowering data engineers to inject specific rendering logic directly into the CAS environment. By defining custom value mappings—such as translating binary codes into descriptive text or grouping numerical ranges into categories—you ensure that downstream analytics and visual reports are both accurate and user-friendly. Below, we have compiled a comprehensive Q&A resource to help you master the syntax and application of these custom definitions within your data workflows.

Description

The `addFormat` action is a powerful tool within the `sessionProp` action set. It allows for the dynamic creation and addition of a new user-defined format to a specified format library within the current CAS session. This is particularly useful for creating custom data representations on-the-fly without needing to pre-define them in a SAS catalog. You can define simple value mappings or complex ranges, and even create picture formats for detailed numeric formatting. The created formats are immediately available for use in the session where they were defined.

sessionProp.addFormat { dataType={ "string-1" <, "string-2", ...> }, defaultL=64-bit-integer, fill={ "string-1" <, "string-2", ...> }, fmtLibName="string", fmtName="string", fmtType="string", fuzz=double, locale="string", maxL=64-bit-integer, minL=64-bit-integer, mult={ double-1 <, double-2, ...> }, multiLabel=TRUE | FALSE, noedit={ "string-1" <, "string-2", ...> }, notSorted=TRUE | FALSE, prefix={ "string-1" <, "string-2", ...> }, ranges={ "string-1" <, "string-2", ...> }, replace=TRUE | FALSE };
Settings
ParameterDescription
dataType Indicates whether the value is of type DATE, TIME, or DATETIME.
defaultL Specifies the default length of the format.
fill Indicates the fill character for a PICTURE format.
fmtLibName Specifies the name of the format library where the new format will be stored.
fmtName Specifies the name for the new format. A character format name must start with a '$'.
fmtType Indicates the type of format: PICTURE, INVALUE, or VALUE.
fuzz Specifies a fuzz factor for matching numeric values to a range. Values within the fuzz factor of a range endpoint are included in the range.
locale Specifies the locale to be used in the format name's locale prefix, enabling culture-specific formatting.
maxL Specifies the maximum length for the format, in bytes.
minL Specifies the minimum length for the format, in bytes.
mult Indicates a multiplier for a PICTURE format, used instead of computing from decimal points.
multiLabel If set to True, allows multiple labels to be specified for a single internal value.
noedit Indicates a non-picture label for a PICTURE format.
notSorted If set to True, values or ranges are stored in the order they are defined, rather than being sorted.
prefix Indicates prefix characters for a PICTURE format.
ranges Specifies a list of value-to-label or range-to-label mappings. For example, `{"1='Low'", "2-5='Medium'", "6-10='High'"}`.
replace If set to True, an existing format of the same name will be overwritten with the new definition.
Data Preparation View data prep sheet
Data Creation

First, we create a format library named 'myFmtLib' to store our custom formats. Then, we load a simple dataset into a CAS table named 'ProductSales'. This table contains product IDs and their corresponding sales figures, which we will use to apply our custom formats.

Copied!
1PROC CAS; SESSION casauto; ACTION sessionProp.addFmtLib / fmtLibName='myFmtLib', replace=true; RUN; DATA casuser.ProductSales; INFILE DATALINES; INPUT ProductID Sales; DATALINES;
21 150
32 2500
43 800
54 5500
65 450
7;
8RUN; QUIT;

Examples

This example demonstrates how to create a basic numeric format named 'SalesFormat'. This format categorizes sales figures into 'Low', 'Medium', and 'High'. We add this format to our 'myFmtLib' library.

SAS® / CAS Code Code awaiting community validation
Copied!
1 
2PROC CAS;
3ACTION sessionProp.addFormat / fmtLibName='myFmtLib' fmtName='SalesFormat' ranges={'1-1000=Low', '1001-5000=Medium', '5001-high=High'};
4 
5RUN;
6 
7QUIT;
8 
Result :
The action successfully adds the 'SalesFormat' to the 'myFmtLib' format library. A confirmation message is displayed in the log, and the format is now ready to be used in the session.

This example creates a character format '$ProdName' in the 'myFmtLib' library. It maps product IDs to their names. The `replace=true` option ensures that if a format with the same name already exists, it will be overwritten. This is useful for updating format definitions during development.

SAS® / CAS Code Code awaiting community validation
Copied!
1 
2PROC CAS;
3ACTION sessionProp.addFormat / fmtLibName='myFmtLib' fmtName='$ProdName' ranges={'1=Laptop', '2=Desktop', '3=Tablet', '4=Monitor', 'other=Unknown'} replace=true;
4 
5RUN;
6 
7QUIT;
8 
Result :
The '$ProdName' format is successfully created or replaced in the 'myFmtLib' library. The log will show a success status. This format can now be applied to the 'ProductID' column to display product names instead of IDs.

This example shows how to create a sophisticated numeric picture format for currency, specific to the German (Germany) locale. It defines a format named 'EUROFMT' that includes a Euro symbol prefix, uses a dot as the thousands separator, and a comma for the decimal point, which is standard for this locale. The `locale='de_DE'` parameter is key to this functionality.

SAS® / CAS Code Code awaiting community validation
Copied!
1 
2PROC CAS;
3ACTION sessionProp.addFormat / fmtLibName='myFmtLib' fmtName='EUROFMT' fmtType='PICTURE' defaultL=16 ranges={'low-high=000.000,00'} prefix={'€ '} locale='de_DE';
4 
5RUN;
6 
7QUIT;
8 
Result :
A locale-specific picture format 'EUROFMT' is created. When applied to numeric data, it will format numbers according to German currency standards (e.g., 12345.67 becomes '€ 12.345,67'). The log confirms the successful addition of the format.

FAQ

What is the purpose of the `addFormat` action in SAS Viya?
What are the mandatory parameters for using the `addFormat` action?
How do you define the value-to-label mappings for a new format?
Can the `addFormat` action overwrite an existing format with the same name?
What is the function of the `fuzz` parameter?
How can I create a format specific to a language or region?
What are the restrictions on naming a new format with `fmtName`?

Associated Scenarios

Use Case
Standard Case: Customer Risk Profile Segmentation

A financial institution needs to classify its customers into risk categories based on their credit score. This allows for tailored product offerings and risk management. The for...

Use Case
Complex Case: Locale-Specific Picture Format for Financial Reporting

A global e-commerce company needs to generate financial reports where sales figures are displayed in the correct, locale-specific currency format. This involves using different ...

Use Case
Edge Case: Sensor Reading Classification with Fuzz Factor

In a manufacturing setting, IoT sensors monitor machine operating temperatures. Due to minor sensor inaccuracies, readings can be very close to a threshold. We need to classify ...