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!
options casopts=(caslib=casuser timeout=900);
cas casauto;
proc cas;
session casauto.addcaslib / caslib='myglobalformats' path='&_TEMP_/myformats' subdirs=true datasource=(srctype='path');
quit;
proc format lib=myglobalformats;
value $genderfmt
'M' = 'Masculin'
'F' = 'Féminin';
value agegrpfmt
low-<18 = 'Mineur'
18-high = 'Adulte';
run;
proc cas;
session casauto.addFmtSearchPath / searchPath={'myglobalformats'};
quit;
data casuser.clients (promote=yes);
input Name $ Gender $ Age;
format Gender $genderfmt. Age agegrpfmt.;
datalines;
Alice F 25
Bob M 17
Carol F 30
David M 20
;
run;
proc print data=casuser.clients;
run;
proc cas;
session casauto.removeFmtSearchPath / searchPath={'myglobalformats'};
session casauto.dropcaslib / caslib='myglobalformats';
quit;
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!
options casopts=(caslib=casuser timeout=900);
cas casauto;
proc cas;
session casauto.addcaslib / caslib='myglobalformats' path='&_TEMP_/myformats' subdirs=true datasource=(srctype='path');
quit;
proc format lib=myglobalformats;
value $statusfmt
'A' = 'Actif'
'I' = 'Inactif'
'X' = 'En attente'
other = 'Inconnu';
run;
proc cas;
session casauto.addFmtSearchPath / searchPath={'myglobalformats'};
quit;
data casuser.ventes (promote=yes);
input Produit $ StatutCommande $ Montant;
format StatutCommande $statusfmt.;
datalines;
PC A 1200
Souris I 25
Clavier A 80
Ecran X 300
;
run;
proc print data=casuser.ventes;
run;
proc cas;
session casauto.removeFmtSearchPath / searchPath={'myglobalformats'};
session casauto.dropcaslib / caslib='myglobalformats';
quit;
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!
options casopts=(caslib=casuser timeout=900);
cas casauto;
proc cas;
session casauto.addcaslib / caslib='dynamicformats' path='&_TEMP_/dynamicformats' subdirs=true datasource=(srctype='path');
quit;
%macro create_and_promote_format(libname, fmtname);
proc format lib=&libname;
value &fmtname
1 = 'Faible'
2 = 'Moyen'
3 = 'Élevé';
run;
proc cas;
session casauto.addFmtSearchPath / searchPath={'&libname'};
quit;
%mend;
%create_and_promote_format(libname=dynamicformats, fmtname=level);
proc format lib=dynamicformats;
value level
1 = 'Bas'
2 = 'Moy'
3 = 'Haut';
run;
data casuser.evaluations;
input ID Niveau;
format Niveau level.;
datalines;
101 1
102 2
103 3
;
run;
proc print data=casuser.evaluations;
run;
proc cas;
session casauto.removeFmtSearchPath / searchPath={'dynamicformats'};
session casauto.dropcaslib / caslib='dynamicformats';
quit;
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.
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.
« 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. »
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.