Published on :

Managing SAS Formats in CAS

This code is also available in: Deutsch Español Français
Awaiting validation
Attention : This code requires administrator privileges.
The script initializes a CAS session and assigns all available caslibs. It lists existing CAS format libraries, then creates a temporary dataset 'cars' from 'sashelp.cars'. A SAS©9 format named 'enginesize' is created locally, and a copy is automatically created in a dedicated CAS format library ('casformats'). This format library is then promoted to make it global and persistent in memory. The script saves the format library to a SASHDAT file to ensure its persistence beyond CAS server restarts, then verifies the file's existence. A CAS dataset is then created, using the defined format. Finally, the script shows how to delete session and global format libraries, then how to reload a format from the SASHDAT file, listing the formats and their values to validate the operation.
Data Analysis

Type : MIXTE


The script uses the SASHELP.CARS dataset, an internal SAS data source, for the initial data creation. Temporary datasets ('cars') and CAS datasets ('casuser.cars_cas') are created internally and used to apply the formats.

1 Code Block
CASL
Explanation :
This block initializes a Cloud Analytic Services (CAS) session and assigns all available caslibs, allowing access to CAS data and resources. It then lists all currently loaded CAS format libraries, providing an overview of the initial state of the formats.
Copied!
1cas;
2caslib _all_ assign;
3 
4cas casauto listformats;
2 Code Block
DATA STEP Data
Explanation :
Creates a temporary dataset named 'cars' in the WORK library from the SASHELP.CARS dataset. This dataset will subsequently be used to create formats and CAS tables.
Copied!
1DATA cars;
2 SET sashelp.cars;
3RUN;
3 Code Block
PROC FORMAT
Explanation :
This block defines a user-defined format 'enginesize' in the WORK.FORMATS library. Thanks to the `casfmtlib="casformats"` option, a copy of this format is automatically created and added to a new CAS format library named 'casformats'. This format categorizes engine sizes into different descriptive categories.
Copied!
1PROC FORMAT library=work.formats casfmtlib="casformats" ;
2 value enginesize
3 low - <2.7 = "Very economical"
4 2.7 - <4.1 = "Small"
5 4.1 - <5.5 = "Medium"
6 5.5 - <6.9 = "Large"
7 6.9 - high = "Very large";
8RUN;
4 Code Block
CASL
Explanation :
Lists the CAS format libraries again to confirm the creation of the 'casformats' library following the execution of PROC FORMAT.
Copied!
1cas casauto listformats;
5 Code Block
CASL
Explanation :
Promotes the 'casformats' format library to make it global and persistent in CAS memory. This allows other CAS users or sessions to access this format without having to redefine it.
Copied!
1cas casauto promotefmtlib fmtlibname='casformats';
6 Code Block
CASL
Explanation :
Lists the format libraries after promotion to check the status of the 'casformats' library (it should appear as promoted).
Copied!
1cas casauto listformats;
7 Code Block
CASL
Explanation :
Saves the 'casformats' format library to a SASHDAT file named 'casfmt_table' within the 'formats' caslib. This operation ensures the persistence of the format beyond CAS server restarts.
Copied!
1cas casauto savefmtlib fmtlibname='casformats' caslib='formats' TABLE='casfmt_table' replace;
2 
8 Code Block
PROC CASUTIL
Explanation :
Uses PROC CASUTIL to list the files present in the 'formats' caslib and confirm that the SASHDAT file 'casfmt_table' containing the formats has indeed been created and is accessible.
Copied!
1 
2PROC CASUTIL incaslib='formats';
3list files;
4QUIT;
5 
9 Code Block
DATA STEP Data
Explanation :
Creates a CAS dataset named 'cars_cas' in the 'casuser' caslib from SASHELP.CARS, and applies the 'enginesize' format to the 'EngineSize' variable. The block then displays the first 10 observations of the new dataset to verify the correct application of the CAS format.
Copied!
1DATA casuser.cars_cas(replace=yes);
2 SET sashelp.cars;
3 FORMAT enginesize enginesize.;
4RUN;
5 
6PROC PRINT DATA=casuser.cars_cas(obs=10);
7var EngineSize;
8RUN;
10 Code Block
CASL
Explanation :
Lists the CAS format libraries again for a status update after format usage.
Copied!
1cas casauto listformats;
11 Code Block
CASL
Explanation :
Deletes the 'CASFORMATS' format library from the active CAS session. The `fmtsearchremove` option ensures it is also removed from format search paths.
Copied!
1cas casauto dropfmtlib fmtlibname=CASFORMATS fmtsearchremove;
2 
12 Code Block
CASL
Explanation :
Lists the CAS format libraries to confirm that 'CASFORMATS' has been removed from the session.
Copied!
1cas casauto listformats;
13 Code Block
CASL
Explanation :
Attempts to delete the 'CASFORMATS' format library again. The original code comment indicates that a warning message may appear if the library is no longer found, but this command aims to ensure it is thoroughly removed from any context.
Copied!
1cas casauto dropfmtlib fmtlibname=CASFORMATS fmtsearchremove;
2 
14 Code Block
CASL
Explanation :
Lists the CAS format libraries to check the result of the global deletion attempt.
Copied!
1cas casauto listformats;
15 Code Block
CASL
Explanation :
Loads a format library from the SASHDAT file 'casfmt_table' (previously saved) and makes it available under the new name 'fmthdat' in the CAS session.
Copied!
1cas casauto addfmtlib fmtlibname=fmthdat
2caslib=formats
3TABLE=casfmt_table;
4 
16 Code Block
CASL
Explanation :
Lists the CAS format libraries to confirm the loading of 'fmthdat'.
Copied!
1cas casauto listformats;
17 Code Block
CASL
Explanation :
Displays the members (individual formats) of all loaded format libraries, then specifically lists the value ranges and labels defined for the 'enginesize' format, validating its content and availability.
Copied!
1cas casauto listformats members;
2cas casauto listfmtranges fmtname=enginesize;
3 
18 Code Block
DATA STEP Data
Explanation :
Recreates the CAS dataset 'cars_cas' using the 'enginesize' format that was reloaded from the SASHDAT file, demonstrating that the format is again active and usable after loading from persistent storage.
Copied!
1DATA casuser.cars_cas(replace=yes);
2 SET sashelp.cars;
3 FORMAT enginesize enginesize.;
4RUN;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : Copyright © 2021, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. SPDX-License-Identifier: Apache-2.0