Published on :
Administration CREATION_INTERNE

Promote a Format Library

This code is also available in: Deutsch Español Français
Awaiting validation
Attention : This code requires administrator privileges.
This feature allows promoting a SAS© format library (often created via PROC FORMAT) to a global caslib or making it accessible to other CAS users. This is crucial in a distributed environment like SAS© Viya to ensure that custom formats are recognized and applied correctly when processing in-memory data in CAS. 'Promotion' in this context means making these formats persistent and shareable within the CAS environment, typically by storing them in an appropriate caslib. This avoids having to redefine formats for each session or for each user and ensures consistency in data interpretation.
Data Analysis

Type : CREATION_INTERNE


Examples use generated data (datalines) or SASHELP to create and promote a format library directly within caslibs.

1 Code Block
PROC FORMAT, PROC CAS Data
Explanation :
This example illustrates the most common way to 'promote' formats in SAS Viya: by creating them directly within a caslib designated for formats (here, 'myglobalformats'). This makes the '$genderfmt' and 'agegrpfmt' formats immediately available for all subsequent operations in the specified caslib and potentially for other users if the caslib is global and permanent. It defines formats and applies them to a new CAS table.
Copied!
1options casopts=(caslib=casuser timeout=900);
2cas casauto;
3 
4PROC CAS;
5 SESSION casauto.addcaslib / caslib='myglobalformats' path='&_TEMP_/myformats' subdirs=true datasource=(srctype='path');
6QUIT;
7 
8PROC FORMAT lib=myglobalformats;
9 value $genderfmt
10 'M' = 'Masculin'
11 'F' = 'Féminin';
12 value agegrpfmt
13 low-<18 = 'Mineur'
14 18-high = 'Adulte';
15RUN;
16 
17PROC CAS;
18 SESSION casauto.addFmtSearchPath / searchPath={'myglobalformats'};
19QUIT;
20 
21DATA casuser.clients (promote=yes);
22 INPUT Name $ Gender $ Age;
23 FORMAT Gender $genderfmt. Age agegrpfmt.;
24DATALINES;
25Alice F 25
26Bob M 17
27Carol F 30
28David M 20
29;
30RUN;
31 
32PROC PRINT DATA=casuser.clients;
33RUN;
34 
35PROC CAS;
36 SESSION casauto.removeFmtSearchPath / searchPath={'myglobalformats'};
37 SESSION casauto.dropcaslib / caslib='myglobalformats';
38QUIT;
2 Code Block
PROC FORMAT, DATA STEP Data
Explanation :
This example simulates the use of a '$statusfmt' format that has been 'promoted' (made available in a 'myglobalformats' caslib). It shows how to ensure that the caslib containing the formats is in the CAS session's format search path (`addFmtSearchPath`) and then how to apply this format to new data created in a DATA step running in CAS mode. It highlights the accessibility of the format after its 'promotion'.
Copied!
1options casopts=(caslib=casuser timeout=900);
2cas casauto;
3 
4PROC CAS;
5 SESSION casauto.addcaslib / caslib='myglobalformats' path='&_TEMP_/myformats' subdirs=true datasource=(srctype='path');
6QUIT;
7 
8PROC FORMAT lib=myglobalformats;
9 value $statusfmt
10 'A' = 'Actif'
11 'I' = 'Inactif'
12 'X' = 'En attente'
13 other = 'Inconnu';
14RUN;
15 
16PROC CAS;
17 SESSION casauto.addFmtSearchPath / searchPath={'myglobalformats'};
18QUIT;
19 
20DATA casuser.ventes (promote=yes);
21 INPUT Produit $ StatutCommande $ Montant;
22 FORMAT StatutCommande $statusfmt.;
23DATALINES;
24PC A 1200
25Souris I 25
26Clavier A 80
27Ecran X 300
28;
29RUN;
30 
31PROC PRINT DATA=casuser.ventes;
32RUN;
33 
34PROC CAS;
35 SESSION casauto.removeFmtSearchPath / searchPath={'myglobalformats'};
36 SESSION casauto.dropcaslib / caslib='myglobalformats';
37QUIT;
3 Code Block
PROC FORMAT, %MACRO, PROC CAS Data
Explanation :
This advanced example uses a SAS macro to dynamize the creation and 'promotion' of a format. It shows how a format can be created and made available in a caslib via a macro. Furthermore, it illustrates a conflict management scenario where a format with the same name is redefined, and how the system uses the most recent or highest priority definition in the format search path.
Copied!
1options casopts=(caslib=casuser timeout=900);
2cas casauto;
3 
4PROC CAS;
5 SESSION casauto.addcaslib / caslib='dynamicformats' path='&_TEMP_/dynamicformats' subdirs=true datasource=(srctype='path');
6QUIT;
7 
8%macro create_and_promote_format(LIBNAME, fmtname);
9 PROC FORMAT lib=&LIBNAME;
10 value &fmtname
11 1 = 'Faible'
12 2 = 'Moyen'
13 3 = 'Élevé';
14 RUN;
15
16 PROC CAS;
17 SESSION casauto.addFmtSearchPath / searchPath={'&libname'};
18 QUIT;
19%mend;
20 
21%create_and_promote_format(LIBNAME=dynamicformats, fmtname=level);
22 
23PROC FORMAT lib=dynamicformats;
24 value level
25 1 = 'Bas'
26 2 = 'Moy'
27 3 = 'Haut';
28RUN;
29 
30DATA casuser.evaluations;
31 INPUT ID Niveau;
32 FORMAT Niveau level.;
33DATALINES;
34101 1
35102 2
36103 3
37;
38RUN;
39 
40PROC PRINT DATA=casuser.evaluations;
41RUN;
42 
43PROC CAS;
44 SESSION casauto.removeFmtSearchPath / searchPath={'dynamicformats'};
45 SESSION casauto.dropcaslib / caslib='dynamicformats';
46QUIT;
4 Code Block
PROC FORMAT, PROC CAS, DATA STEP Data
Explanation :
This example focuses on managing the lifecycle of 'promoted' formats in SAS Viya. It shows how to check which caslibs are currently included in a CAS session's format search path via `listFmtSearchPath`. Then, it demonstrates how to explicitly remove a caslib from the search path (`removeFmtSearchPath`) and how to clean up temporary resources (the caslib itself) after use, which is essential for resource management in a shared CAS environment.
Copied!
1options casopts=(caslib=casuser timeout=900);
2cas casauto;
3 
4PROC CAS;
5 SESSION casauto.addcaslib / caslib='temp_formats' path='&_TEMP_/temp_formats' subdirs=true datasource=(srctype='path');
6QUIT;
7 
8PROC FORMAT lib=temp_formats;
9 value $yesnofmt
10 'Y' = 'Oui'
11 'N' = 'Non';
12RUN;
13 
14PROC CAS;
15 SESSION casauto.addFmtSearchPath / searchPath={'temp_formats'};
16QUIT;
17 
18PROC CAS;
19 SESSION casauto.listFmtSearchPath;
20QUIT;
21 
22DATA casuser.sondage;
23 INPUT Question $ Reponse $;
24 FORMAT Reponse $yesnofmt.;
25DATALINES;
26Q1 Y
27Q2 N
28;
29RUN;
30 
31PROC PRINT DATA=casuser.sondage;
32RUN;
33 
34PROC CAS;
35 SESSION casauto.removeFmtSearchPath / searchPath={'temp_formats'};
36QUIT;
37 
38PROC CAS;
39 SESSION casauto.listFmtSearchPath;
40QUIT;
41 
42PROC CAS;
43 SESSION casauto.dropcaslib / caslib='temp_formats' _all_=true;
44QUIT;
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.
Banner
Expert Advice
Expert
Michael
Responsable de l'infrastructure Viya.
« When working in a multi-user environment, always check the current search order using the listFmtSearchPath action. If two format libraries contain the same format name, CAS will use the one found first in the search path, which can lead to silent data interpretation errors if not properly managed. »