?> addFormat - WeAreCAS
sessionProp

addFormat

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
dataTypeIndicates whether the value is of type DATE, TIME, or DATETIME.
defaultLSpecifies the default length of the format.
fillIndicates the fill character for a PICTURE format.
fmtLibNameSpecifies the name of the format library where the new format will be stored.
fmtNameSpecifies the name for the new format. A character format name must start with a '$'.
fmtTypeIndicates the type of format: PICTURE, INVALUE, or VALUE.
fuzzSpecifies a fuzz factor for matching numeric values to a range. Values within the fuzz factor of a range endpoint are included in the range.
localeSpecifies the locale to be used in the format name's locale prefix, enabling culture-specific formatting.
maxLSpecifies the maximum length for the format, in bytes.
minLSpecifies the minimum length for the format, in bytes.
multIndicates a multiplier for a PICTURE format, used instead of computing from decimal points.
multiLabelIf set to True, allows multiple labels to be specified for a single internal value.
noeditIndicates a non-picture label for a PICTURE format.
notSortedIf set to True, values or ranges are stored in the order they are defined, rather than being sorted.
prefixIndicates prefix characters for a PICTURE format.
rangesSpecifies a list of value-to-label or range-to-label mappings. For example, `{"1='Low'", "2-5='Medium'", "6-10='High'"}`.
replaceIf set to True, an existing format of the same name will be overwritten with the new definition.
Data Preparation
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.

proc cas; session casauto; action sessionProp.addFmtLib / fmtLibName='myFmtLib', replace=true; run; data casuser.ProductSales; infile datalines; input ProductID Sales; datalines;
1 150
2 2500
3 800
4 5500
5 450
;
run; 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
Copied!
1PROC CAS; ACTION sessionProp.addFormat / fmtLibName='myFmtLib' fmtName='SalesFormat' ranges={'1-1000=Low', '1001-5000=Medium', '5001-high=High'}; RUN; QUIT;
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
Copied!
1PROC CAS; ACTION sessionProp.addFormat / fmtLibName='myFmtLib' fmtName='$ProdName' ranges={'1=Laptop', '2=Desktop', '3=Tablet', '4=Monitor', 'other=Unknown'} replace=true; RUN; QUIT;
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
Copied!
1PROC CAS; ACTION sessionProp.addFormat / fmtLibName='myFmtLib' fmtName='EUROFMT' fmtType='PICTURE' defaultL=16 ranges={'low-high=000.000,00'} prefix={'€ '} locale='de_DE'; RUN; QUIT;
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 ...