Casformats

Download as pdf or txt
Download as pdf or txt
You are on page 1of 38

®

SAS Cloud Analytic


Services 3.5: User-Defined
Formats

SAS® Documentation
March 13, 2020
The correct bibliographic citation for this manual is as follows: SAS Institute Inc. 2019. SAS® Cloud Analytic Services 3.5: User-Defined
Formats. Cary, NC: SAS Institute Inc.
SAS® Cloud Analytic Services 3.5: User-Defined Formats
Copyright © 2019, SAS Institute Inc., Cary, NC, USA

All Rights Reserved. Produced in the United States of America.


For a hard copy book: No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any
means, electronic, mechanical, photocopying, or otherwise, without the prior written permission of the publisher, SAS Institute Inc.
For a web download or e-book: Your use of this publication shall be governed by the terms established by the vendor at the time you
acquire this publication.
The scanning, uploading, and distribution of this book via the Internet or any other means without the permission of the publisher is illegal and
punishable by law. Please purchase only authorized electronic editions and do not participate in or encourage electronic piracy of copyrighted
materials. Your support of others' rights is appreciated.
U.S. Government License Rights; Restricted Rights: The Software and its documentation is commercial computer software developed at
private expense and is provided with RESTRICTED RIGHTS to the United States Government. Use, duplication, or disclosure of the Software
by the United States Government is subject to the license terms of this Agreement pursuant to, as applicable, FAR 12.212, DFAR
227.7202-1(a), DFAR 227.7202-3(a), and DFAR 227.7202-4, and, to the extent required under U.S. federal law, the minimum restricted rights
as set out in FAR 52.227-19 (DEC 2007). If FAR 52.227-19 is applicable, this provision serves as notice under clause (c) thereof and no other
notice is required to be affixed to the Software or documentation. The Government’s rights in Software and documentation shall be only those
set forth in this Agreement.
SAS Institute Inc., SAS Campus Drive, Cary, NC 27513-2414
March 2020
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.
Other brand and product names are trademarks of their respective companies.
3.5-P2:casformats
Contents
Chapter 1 / About User-Defined Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
User-Defined Format Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Permanent Formats Supplied by SAS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
How Are Format Libraries Different than Format Catalogs? . . . . . . . . . . . . . . . . . . . . . 2

Chapter 2 / Using User-Defined Formats In SAS Cloud Analytic Services . . . . . . . . . . . . . . . . . . . . . . 3


Manage Your User-Defined Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
The %UDFSEL Macro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Add and Save a User-Defined Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Add a Format Library from a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
List Format Libraries, Formats, and Search List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Add Locale-Specific Format Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Migrate User-Defined Formats from SAS To CAS . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Add Formats Referenced in a Single SAS Library to a CAS Session . . . . . . . . . . . . 21
Promote a Format Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Add a Global Format Library to a Session’s Format Search List . . . . . . . . . . . . . . . . 27
Drop a Format Library from Session Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

Chapter 3 / Utility Macro for User-Defined Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31


Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
iv Contents
1

1
About User-Defined Formats

User-Defined Format Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1


Permanent Formats Supplied by SAS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
How Are Format Libraries Different than Format Catalogs? . . . . . . . . . . . . . . . . . . . . . . 2

User-Defined Format Basics


SAS provides formats for controlling how variables are printed. You can use the
FORMAT procedure in your SAS programs to create your own formats. The
FORMAT procedure supports the creation of user-defined formats in catalogs on the
SAS client. It also supports adding formats and format libraries for a SAS Cloud
Analytic Services session. User-defined format names must comply with the
following rules:
n The name must be a valid SAS name. See “Names in the SAS Language” in
SAS Language Reference: Concepts.
n The name of a numeric format must be 1 to 32 characters in length. The name of
a character format must be the $ character followed by 1 to 31 characters such
as $DepartmentName.
n The name cannot be the same as the name of a format that is supplied by SAS.
See “Dictionary of Formats” in SAS Formats and Informats: Reference.
n The name cannot end in a number.

Information about working with user-defined formats on the SAS client are described
in FORMAT procedure. The information in this section covers how to work with user-
defined formats with SAS Cloud Analytic Services.
A format library has a name and can be temporary (session-scope) for the duration
of your programming session. The alternative is that it can be set to have global
scope and exist for as long as the server is running. You can persist a format library
in a SASHDAT file and you can add formats to your CAS session from a persisted
file.
Your administrator can configure the server so that the following occur when the
server starts:
n format libraries are added and promoted from persisted format libraries (libraries
that are persisted as SASHDAT files in a caslib's data source).
n during session start up for each connection, the format search path is updated to
include the format libraries.
2 Chapter 1 / About User-Defined Formats

For information about how to perform common tasks related to user-defined formats
in SAS Cloud Analytic Services, see “Manage Your User-Defined Formats” on page
4.

Permanent Formats Supplied by SAS


In addition to user-defined formats, SAS provides formats that can be used with
SAS Cloud Analytic Services. For a list of formats that are supported, see SAS
Formats and Informats: Reference.

How Are Format Libraries Different than Format


Catalogs?
Format catalogs are accessed by the SAS client and are used for formatting values
in procedures and DATA steps that run on the SAS client. These procedures include
PRINT, SQL, and so on. Format libraries are used within SAS Cloud Analytic
Services. These are server-side formats that the server uses when an analysis is
performed according to formatted values. For example, when a procedure or DATA
step that runs in the server works on formatted values, the format must be available
in a format library. These procedures include CARDINALITY, BINNING, and so on,
which are documented in SAS Visual Statistics: Procedures Guide.
For procedures that run in the server, you must assign a format to a variable before
the table is loaded into the server. After a table is in memory, you cannot assign
formats to the table variables. To apply formats to table variables before the table is
loaded, use a FORMAT statement in a DATA step or with the CASUTIL procedure.
See “Migrate User-Defined Formats from SAS To CAS” for an example of migrating
your personal format catalogs to format libraries. See “Add a Format Library from a
File” on page 9 for an example of adding a format library that is saved to a file.
3

2
Using User-Defined Formats In SAS
Cloud Analytic Services

Manage Your User-Defined Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4


The %UDFSEL Macro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
About the %UDFSEL Macro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Definition and Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Add and Save a User-Defined Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Key Ideas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Add a Format Library from a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Key Ideas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
List Format Libraries, Formats, and Search List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
SAS Log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Key Ideas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Add Locale-Specific Format Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
SAS Log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Key Ideas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Migrate User-Defined Formats from SAS To CAS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Example 1: Migrate User-Defined Formats Using the FORMAT Procedure . . . . . . . 18
Example 2: Migrate User-Defined Formats Using the FMTC2ITM Procedure . . . . . 19
Key Ideas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Add Formats Referenced in a Single SAS Library to a CAS Session . . . . . . . . . . . . . 21
Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
SAS Log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Key Ideas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Promote a Format Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
SAS Log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Key Ideas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Add a Global Format Library to a Session’s Format Search List . . . . . . . . . . . . . . . . . 27
Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
4 Chapter 2 / Using User-Defined Formats In SAS Cloud Analytic Services

SAS Log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Key Ideas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Drop a Format Library from Session Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
SAS Log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Key Ideas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

Manage Your User-Defined Formats


If you are using a SAS client to access SAS Cloud Analytic Services, you can use
the FORMAT procedure, the FMTC2ITM procedure, and the CAS statement to
manage your user-defined formats, or you can use SAS Cloud Analytic Services
actions. If you are using a Python, Lua, or R client to access the server, use the
actions to manage your user-defined formats. Table 2.1 on page 4 lists common
tasks for user-defined formats that you can accomplish using the FORMAT
procedure and the CAS statement on a SAS client.
For information about using the actions to manage your user-defined formats, see
“Managing User-Defined Formats Using SAS Cloud Analytic Services Actions” in
SAS Viya: System Programming Guide. If your site has format catalogs that are
used widely by many programmers, see Managing User-Defined Formats in SAS
Viya.

Table 2.1 Performing Common Tasks for User-Defined Formats Using the FORMAT Procedure and CAS
Statement

Task How to Complete Example

Add a new format to SAS and Use the FORMAT procedure “Add and Save a User-Defined
SAS Cloud Analytic Services. with the CASFMTLIB= option. Format” (p. 6)

Display your format search list. Use a CAS statement with the “List Format Libraries, Formats, and
LISTFMTSEARCH option. Search List” (p. 12)

List the format libraries that are Use a CAS statement with the “List Format Libraries, Formats, and
available. LISTFORMATS option. Search List” (p. 12)

Add a global format library to Use a CAS statement with the “Add a Global Format Library to a
your format search list. FMTSEARCH option. Session’s Format Search List” (p. 27)

List the format names in all Use a CAS statement with the “List Format Libraries, Formats, and
format libraries. LISTFORMATS and MEMBERS Search List” (p. 12)
options.
Manage Your User-Defined Formats 5

Task How to Complete Example

Copy existing SAS format Do one of the following: “Migrate User-Defined Formats from
catalogs to a SAS Cloud Analytic SAS To CAS” (p. 18)
n Use the FORMAT procedure
Services format library.
to write information about
formats in a format catalog to
a temporary SAS data set,
and then to read the format
information from the
temporary data set into a
CAS format library.
n Use the FMTC2ITM
procedure to copy the SAS
user-defined format catalogs
to an item-store file, and then
use a CAS statement with the
ADDFMTLIB and the PATH=
options to add the formats in
the item-store file to a CAS
format library.

Convert your existing SAS See “Convert Format Catalogs


format catalogs that were to UTF-8” in Migrating Data to
created in non-UTF-8 SAS UTF-8 for SAS Viya.
sessions to UTF-8 encoding.

Copy only the formats that are Do the following: “Add Formats Referenced in a Single
referenced in data sets stored in SAS Library to a CAS Session” (p.
n Use the %UDFSEL macro on
a single SAS library to a SAS 21)
page 6 to create a
Cloud Analytic Services format
SELECT statement for all
library.
user-defined formats
referenced in the library.
n Use the FORMAT procedure
to get format data using the
SELECT statement and to
store the data in a table.
n Use the FORMAT procedure
to copy the format data table
to a SAS Cloud Analytic
Services format library.

Persist a format library as a Use a CAS statement with the “Add and Save a User-Defined
SASHDAT file. SAVEFMTLIB and TABLE Format” (p. 6)
options, and, if needed, the
CASLIB option.

Add a format library that is stored Use the CAS statement with the “Add a Format Library from a File” (p.
in a SASHDAT file. ADDFMTLIB and TABLE 9)
options, and, if needed, the
CASLIB option.
6 Chapter 2 / Using User-Defined Formats In SAS Cloud Analytic Services

Task How to Complete Example

Drop a format library from Use the CAS statement with the “Drop a Format Library from Session
session scope. DROPFMTLIB and Scope” (p. 29)
FMTLIBNAME options.

The %UDFSEL Macro

About the %UDFSEL Macro


The %UDFSEL macro is a utility macro that you can use with the FORMAT
procedure to copy selected user-defined formats from SAS to SAS Cloud Analytic
Services. It scans the data sets in a single SAS library, and then generates a
SELECT statement for all of the unique non-intrinsic formats that are referenced.
You can use the generated SELECT statement with the FORMAT procedure to copy
the user-defined formats that are referenced in the data from SAS to a CAS format
library.

Definition and Usage


If you are using SAS 9.4M5 or a later release, the %UDFSEL macro is already
defined as an autocall macro. Otherwise, you must compile the %UDFSEL macro
before you can use it. The source code is listed in “%UDFSEL Macro Source Code”
on page 32. For syntax and usage information, see “%UDFSEL Autocall Macro” on
page 31.

Add and Save a User-Defined Format

Example
/* options cashost="cloud.example.com" casport=5570; */
cas casauto sessopts=(caslib="casuser"); /* 1 */

proc format library=work.formats casfmtlib="casformats"; /* 2 */


value enginesize
low - <2.7 = "Very economical"
2.7 - <4.1 = "Small"
Add and Save a User-Defined Format 7

4.1 - <5.5 = "Medium"


5.5 - <6.9 = "Large"
6.9 - high = "Very large"
;
run;

cas casauto savefmtlib fmtlibname=casformats /* 3 */


table=enginefmt replace;

proc casutil;
format enginesize enginesize.; /* 4 */
load data=sashelp.cars casout="cars" replace;
contents casdata="cars";
run;
quit;

libname mycas cas; /* 5 */

proc mdsummary data=mycas.cars;


var mpg_highway;
groupby enginesize / out=mycas.mpg_hwy_by_size; /* 6 */
run;

proc print data=mycas.mpg_hwy_by_size;


var enginesize--_mean_;
run;

proc casutil; /* 7 */
save casdata="cars" casout="cars_formatted" replace;
droptable casdata="cars";
run;
quit;

1 The CAS statement starts session Casauto.


2 The FORMAT procedure creates a format that is named Enginesize. On the SAS
client, the format is temporary and is stored in the Work library. The
CASFMTLIB= option adds the same format to the active CAS session in a
format library that is named Casformats.
3 The CAS statement SAVEFMTLIB option saves the format library as a
SASHDAT file in the data source that is associated with the active caslib. The
CASUSER caslib is typically a PATH type and uses a file system. The TABLE=
option specifies the name and results in a file that is named enginefmt.sashdat.
4 The FORMAT statement assigns the Enginesize format to the Enginesize
variable. The FORMAT statement applies to LOAD DATA statements only and
must be submitted before the LOAD DATA statements. Otherwise, it has no
effect. (See “CASUTIL Procedure” in SAS Cloud Analytic Services: User’s Guide
for more information.) The format is applied to the in-memory instance of the
Cars table. See Figure 2.1.
5 A CAS LIBNAME engine libref is assigned so that SAS procedures can access
the Cars table that is in memory on the server. The assignment does not specify
a caslib, so the active caslib is used.
6 The MDSUMMARY procedure provides descriptive statistics for the Cars table.
The results are grouped by the formatted values of the Enginesize variable (five
8 Chapter 2 / Using User-Defined Formats In SAS Cloud Analytic Services

values) instead of the numeric value. As a result, the output table,


Mpg_hwy_by_size, has five rows.
7 Optional: The formatted results are saved to table Cars_formatted in the active
caslib so that you can run the remaining examples in this section.

Results
The following display shows the partial results for the CONTENTS statement in the
CASUTIL procedure. The results show that the EngineSize column uses the
Enginesize format.

Figure 2.1 Partial Results for the CONTENTS Statement of the CASUTIL Procedure

The following display shows the first few columns from the Mpg_hwy_by_size table.

Figure 2.2 Selected Columns from the Mpg_Hwy_By_Size Table


Add a Format Library from a File 9

Key Ideas
n Use the FORMAT procedure with the CASFMTLIB= option to add formats to your
CAS session automatically.
n You can save all the formats in a format library in a SASHDAT file with the CAS
statement and the SAVEFMTLIB option. Only caslibs with path-based data sources
such as PATH, DNFS, and HDFS can be used.
n After you have saved your formats to a SASHDAT file, you can do the following:

o Add the formats to individual programming sessions as shown in the “Add a


Format Library from a File” on page 9 example.
o If you want to make your formats available to all sessions, contact your SAS
Cloud Analytic Services system administrator.

Add a Format Library from a File

Prerequisites
Before you run the code in this example, you must do the following in order to get
the correct results:

1 If you have not already done so, run the code in “Add and Save a User-Defined
Format”.

2 Remove format Enginesize from the SAS client.


proc datasets lib=work noprint;
delete formats / memtype=catalog;
run;

3 Drop format library Casformats from the current SAS Cloud Analytic Services
session.
cas casauto dropfmtlib fmtlibname=casformats;

Example
When you use the CAS statement with the SAVEFMTLIB option, you create a
SASHDAT file that includes the formats in a format library. One requirement is that
you specify a caslib with a data source type that supports saving files. The data
source types are PATH, DNFS, and HDFS.
10 Chapter 2 / Using User-Defined Formats In SAS Cloud Analytic Services

This example shows the reverse of that process, adding a format library from a
SASHDAT file.
cas casauto addfmtlib fmtlibname=fmtlib1 /* 1 */
caslib=casuser
table=enginefmt;

proc casutil; /* 2 */
contents casdata="cars_formatted.sashdat";
load casdata="cars_formatted.sashdat"
casout="cars_formatted";
run;
quit;

libname mycas cas;


proc mdsummary data=mycas.cars_formatted;
var weight length;
groupby enginesize / out=mycas.weight_len_by_size; /* 3 */
run;

proc sort data=mycas.weight_len_by_size /* 4 */


out=work.weight_len_by_size_sorted;
by _Column_ enginesize;
run;

proc print data=weight_len_by_size_sorted; /* 5 */


var enginesize -- _mean_;
by _column_;
run;

1 The CAS statement ADDFMTLIB option adds a format library to the active CAS
session. The file, Enginefmt.sashdat, is specified in the TABLE= option. The file
is relative to the path for the CASUSER caslib.
2 The CAS procedure CONTENTS statement specifies the on-disk name,
Cars_formatted.sashdat. As a result, the column information shows that the
format Enginesize was associated with column Enginesize when the table was
saved.
3 Because the Enginesize format has five values and the VAR statement specifies
two columns, the MDSUMMARY procedure creates an output table,
Weight_len_by_size, with 10 rows.
4 The SORT procedure sorts the results from the MDSUMMARY procedure by
columns _Column_ and Enginesize. The sorted data is stored in table
Work.Weight_len_by_size_sorted.
5 The PRINT procedure prints the sorted data by column _Column_.

Results
The following display shows the results of the PRINT procedure. The rows are
grouped by the two different values of _Column_.
Add a Format Library from a File 11

Key Ideas
n Formats added with the CAS statement are added to SAS Cloud Analytic Services,
but not to SAS. If you want to add formats to both SAS Cloud Analytic Services and
SAS, use the FORMAT procedure as shown in “Add and Save a User-Defined
Format” on page 6.
n Use the CASLIB option in the CAS statement if the file is not in the active caslib.
The caslib must have a path-based data source, such as PATH, DNFS, or HDFS.
n Use the TABLE option in the CAS statement if the name of the file on disk is
different from the name specified in the FMTLIBNAME option.
n By default, the format library is appended to the session’s format search list.
Include the POSITION option in the CAS statement to specify a different location in
the format search list.
n By default, the format library is added in session scope. Include the PROMOTE
option in the CAS statement if you also want to promote the format library.
12 Chapter 2 / Using User-Defined Formats In SAS Cloud Analytic Services

List Format Libraries, Formats, and Search List

Example
/* options cashost="cloud.example.com" casport=5570; */
cas casauto; /* 1 */

proc format lib=work.formats casfmtlib="casformats"; /* 2 */


value ynm
1='yes'
2='no'
3='maybe';
run;

proc format lib=work.formats casfmtlib="fmtlib2"; /* 3 */


value sml
1='small'
2='medium'
3='large';
run;

cas casauto listformats members; /* 4 */

cas casauto listfmtranges fmtname=ynm; /* 5 */

cas casauto dropfmtlib fmtlibname=casformats; /* 6 */

cas casauto listfmtsearch; /* 7 */

1 The CAS statement starts session Casauto.


2 The FORMAT procedure creates a format that is named Ynm. The format is
added to the Work.formats catalog on the SAS client. The format is also added
to the active CAS session in a format library that is named Casformats.
3 The FORMAT procedure is used again to create a format that is named Sml. The
format is added to the Work.formats catalog on the SAS client. The format is
also added to the CAS session in a format library that is named Fmtlib2.
4 The LISTFORMATS option lists the format libraries that are available to the
active CAS session. The MEMBERS option includes the formats in each library
in the list. Both Casformats and Fmtlib2 are shown in the SAS log.
5 The LISTFMTSRANGES option is used with FMTNAME= to print a brief view of
the format values for the Ynm format in the SAS log.
6 The DROPFMTLIB option drops the Casformats format library. By default, the
format library name remains in the format search list. You can specify the
FMTSEARCHREMOVE option to remove the format library from the format
search.
List Format Libraries, Formats, and Search List 13

7 The LISTFMTSEARCH option displays the format libraries that are in the search
list. In this example, because the Casformats library was dropped, but the
FMTSEARCHREMOVE option was not used, the name remains in the search
list. "Not Found" is used to indicate that Casformats is not available as a
session-scope or global-scope format library.
14 Chapter 2 / Using User-Defined Formats In SAS Cloud Analytic Services

SAS Log
1 cas casauto;
...
2
3 proc format lib=work.formats casfmtlib="casformats";
NOTE: Both CAS based formats and catalog-based formats will be written.
The CAS based formats will be written to the session CASAUTO.
4 value ynm
5 1='yes'
6 2='no'
7 3='maybe';
NOTE: Format library CASFORMATS added. Format search update using
parameter APPEND completed.
NOTE: Format YNM has been output.
8 run;
...
9
10 proc format lib=work.formats casfmtlib="fmtlib2";
NOTE: Both CAS based formats and catalog-based formats will be written.
The CAS based formats will be written to the session CASAUTO.
11 value sml
12 1='small'
13 2='medium'
14 3='large';
NOTE: Format library FMTLIB2 added. Format search update using parameter APPEND
completed.
NOTE: Format SML has been output.
15 run;
...
16
17 cas casauto listformats members;
NOTE: Fmtlib = CASFORMATS
Scope = Session
Fmtsearch = YES
Format = ynm
NOTE: Fmtlib = FMTLIB2
Scope = Session
Fmtsearch = YES
Format = sml
NOTE: Request to LISTFORMAT completed for session CASAUTO.
18
19 cas casauto listfmtranges fmtname=ynm;
Format Name Range
YNM 1=yes
2=no
3=maybe
NOTE: Request to LISTFMTRANGES YNM completed for session CASAUTO.
20
21 cas casauto dropfmtlib fmtlibname=casformats;
NOTE: Request to DROPFMTLIB CASFORMATS completed for session CASAUTO.
22
23 cas casauto listfmtsearch;
NOTE: FmtLibName = CASFORMATS
Scope = Not Found
NOTE: FmtLibName = FMTLIB2
Scope = Session
NOTE: Request to LISTFMTSEARCH completed for session CASAUTO.
Add Locale-Specific Format Libraries 15

Key Ideas
n When you use the FORMAT procedure with the CASFMTLIB= option, the format
library and formats are automatically made available to your CAS session.
n Use the LISTFMTSEARCH option in a CAS statement to display the format
libraries that are in the search list.
n You can print a brief view of format values in the SAS log with the CAS statement
LISTFMTRANGES and FMTNAME= options. The format name that you specify
must be in the search list.

Add Locale-Specific Format Libraries

Example
/* options cashost="cloud.example.com" casport=5570; */
cas casauto; /* 1 */

proc format locale library=work.formats casfmtlib="casformats"; /* 2 */


value setting
20.5 - HIGH = 'High'
10.5 - <20.5 = 'Medium'
LOW - <10.5 = 'Low';
run;

options locale=fr_FR; /* 3 */
proc format locale library=work.formats casfmtlib="casformats";
value setting
20.5 - HIGH = 'Haute'
10.5 - <20.5 = 'Moyen'
LOW - <10.5 = 'Bas';
run;

options locale=en_US;

cas casauto listformats members; /* 4 */

cas casauto sessopts=(locale="en_US"); /* 5 */


cas casauto listfmtranges fmtname=setting;

cas casauto sessopts=(locale="fr_FR"); /* 6 */


cas casauto listfmtranges fmtname=setting;

1 The CAS statement starts session Casauto.


16 Chapter 2 / Using User-Defined Formats In SAS Cloud Analytic Services

2 The PROC FORMAT step includes the LOCALE option, which specifies to create
a locale-specific format in the format catalog. In this procedure step, the default
locale is used, which is en_US in this example.
3 The OPTIONS statement sets the locale for the SAS client to French. The
subsequent PROC FORMAT step creates a format that corresponds to the
current SAS locale, fr_FR, in the format catalog. The range values must be
specified using a period as the decimal point. Otherwise, a syntax error occurs. It
will be demonstrated later that the decimal range values are displayed
appropriately in the French locale.
4 The CAS statement LISTFORMATS and MEMBERS options list the format
catalog, Casformats, and the format names, fr_fr-setting and en_us-setting, in
the SAS log.
5 The CAS statement SESSOPTS= option sets the CAS session locale to United
States English. The subsequent CAS statement prints a brief view of the format
values in the SAS log.
6 The CAS statement SESSOPTS= option sets the CAS session locale to French.
The subsequent CAS statement prints a brief view of the format values in the
SAS log. To display the appropriate format values for the French locale, SAS
automatically replaces the period with a comma for the decimal point in the
displayed range values. (See “SAS Log” on page 17.) This change occurs
regardless of the locale in which the format was created.
Add Locale-Specific Format Libraries 17

SAS Log
1 cas casauto;
...
2 options locale=en_US;
3 proc format locale library=work.formats casfmtlib="casformats";
NOTE: Both CAS based formats and catalog-based formats will be
written. The CAS based formats will be written to the session
CASAUTO.
4 value setting
5 20.5 - HIGH = 'High'
6 10.5 - <20.5 = 'Medium'
7 LOW - <10.5 = 'Low';
NOTE: Format library CASFORMATS added. Format search update using
parameter APPEND completed.
NOTE: Format SETTING has been written to WORK.FORMATS_en_US.
8 run;
...
9 options locale=fr_FR;
10 proc format locale library=work.formats casfmtlib="casformats";
NOTE: Both CAS based formats and catalog-based formats will be written.
The CAS based formats will be written to the session CASAUTO.
11 value setting
12 20.5 - HIGH = 'Haute'
13 10.5 - <20.5 = 'Moyen'
14 LOW - <10.5 = 'Bas';
NOTE: Format SETTING has been written to WORK.FORMATS_fr_FR.
15 run;
...
16 options locale=en_US;
17 cas casauto listformats members;
NOTE: Fmtlib = CASFORMATS
Scope = Session
Fmtsearch = YES
Format = en_us-setting
Format = fr_fr-setting
NOTE: Request to LISTFORMAT completed for session CASAUTO.
18 cas casauto sessopts=(locale="en_US");
NOTE: The CAS statement request to update one or more session
options for session CASAUTO completed.
19 cas casauto listfmtranges fmtname=setting;
Format Name Range
SETTING LOW-<10.5=Low
10.5-<20.5=Medium
20.5-HIGH=High

NOTE: Request to LISTFMTRANGES SETTING completed for session CASAUTO.


20 cas casauto sessopts=(locale="fr_FR");
NOTE: The CAS statement request to update one or more session options
for session CASAUTO completed.
21 cas casauto listfmtranges fmtname=setting;
Nom du format Intervalle
SETTING LOW-<10,5=Bas
10,5-<20,5=Moyen
20,5-HIGH=Haute

NOTE: Request to LISTFMTRANGES SETTING completed for session CASAUTO.


18 Chapter 2 / Using User-Defined Formats In SAS Cloud Analytic Services

Key Ideas
n To create a locale-specific format, set SAS system option LOCALE to the desired
locale, and then include the LOCALE option in the FORMAT procedure statement.
n To use a locale-specific format, set session option LOCALE to the desired locale.

n The CAS statement LISTFMTRANGES option automatically displays format range


values that are appropriate for the current locale.

See Also
SAS National Language Support (NLS): Reference Guide

Migrate User-Defined Formats from SAS To


CAS

Example 1: Migrate User-Defined Formats Using the


FORMAT Procedure
This example shows you how to use the FORMAT procedure to migrate user-
defined formats from SAS to CAS using a temporary SAS data set. As in the
previous example, it is assumed that user-defined formats are stored in SAS format
catalogs Work.formats and Orion.mailfmt.
/* options cashost="cloud.example.com" casport=5570; */
cas casauto; /* 1 */

catname work.mycat(myfmts.formats orion.mailfmts); /* 2 */

proc format library=work.mycat cntlout=temp; /* 3 */


run;

proc format cntlin=temp casfmtlib='myfmtlib'; /* 4 */


run;

cas casauto listformats fmtlibname=myfmtlib members; /* 5 */

cas casauto savefmtlib fmtlibname=myfmtlib /* 6 */


caslib=casuser table=myfmtlib replace;

catname work.mycat clear; /* 7 */

1 The CAS statement starts session Casauto.


Migrate User-Defined Formats from SAS To CAS 19

2 The CATNAME statement is used to combine catalogs Myfmts.Formats and


Orion.Mailfmt into the Work.Mycat catalog.
3 The FORMAT procedure uses the combined catalog as input and creates a SAS
data set on the SAS client that stores information about the formats.
4 The second PROC FORMAT step reads the SAS data set and adds a format
library in your session that is named MyFmtLib.
5 The CAS statement with the LISTFORMATS and MEMBERS options lists the
format libraries and their members for verfication.
6 Optional: The CAS statement with the SAVEFMTLIB option saves the format
library as a SASHDAT file in caslib Casuser.
7 The CATNAME statement with the CLEAR option disassociates catref
Work.Mycat.

Example 2: Migrate User-Defined Formats Using the


FMTC2ITM Procedure
This example shows you how to use the FMTC2ITM procedure to migrate user-
defined formats that are stored in one or more SAS format catalogs to a format
library in your CAS session. In this example, it is assumed that user-defined formats
are stored in SAS format catalogs Work.formats and Orion.mailfmt. A format item-
store file is used to perform the migration.
/* options cashost="cloud.example.com" casport=5570; */
cas casauto; /* 1 */

proc fmtc2itm /* 2 */
catalog=(work.formats orion.mailfmts)
itemstore="path-to-item-store-file";
run;

cas casauto addfmtlib fmtlibname=myfmtlib /* 3 */


path="path-to-item-store-file"
replacefmtlib;

cas casauto listformat fmtlibname=myfmtlib /* 4 */


members;

1 The CAS statement starts session Casauto.


2 The FMTC2ITM procedure writes the formats in format catalogs Work.Formats
and Orion.Mailfmts to an item store file.

Note: Use a SELECT statement in the FMTC2ITM procedure step to select a


subset of the formats in the specified format catalogs. See “SELECT Statement”
in Base SAS Procedures Guide.

3 The CAS statement ADDFMTLIB option adds the formats in the item-store file
that was created by the FMTC2ITM procedure into CAS format library Myfmtlib.
The PATH= option specifies the path to the item-store file.
20 Chapter 2 / Using User-Defined Formats In SAS Cloud Analytic Services

Note: Path-to-item-store-file must be readable from the control node of the


server.

4 The CAS statement LISTFORMAT option lists the formats in format library
Myfmtlib to the SAS log for verification.

Key Ideas
n Use the methods demonstrated in these examples to migrate your SAS user-
defined format catalogs to SAS Cloud Analytic Services.
n Use the FMTC2ITM procedure with the ITEMSTORE= option to write the formats in
one or more SAS format catalogs to an item-store file. By default all of the formats
in the specified catalogs are written to an item-store file. Use a SELECT statement
in the FMTC2ITM procedure step if you want to select a subset of the formats in
the input format catalogs. See “FMTC2ITM Procedure” in Base SAS Procedures
Guide.
n Use a CAS statement ADDFMTLIB option with PATH= to add the formats stored in
the item-store file into a CAS format library. The path that you specify must be
readable from the control node of the CAS server.
n If you have multiple SAS format catalogs to migrate, use the CATNAME statement
to combine the format catalogs into one format catalog.
n Use the FORMAT procedure to move the formats in a SAS format catalog to SAS
Cloud Analytic Services in two steps:

1 Get information about the formats from the format catalog, and then store
the information in a temporary SAS data set.

2 Copy the format data from the temporary format information data set to a
format library in SAS Cloud Analytic Services.
n If your SAS format catalogs are very large and you use the formats frequently,
consider saving the CAS format library to a SASHDAT file. Once you have saved it
to a file, you can add it to your future sessions, when needed, as shown in “Add a
Format Library from a File” on page 9.
Add Formats Referenced in a Single SAS Library to a CAS Session 21

Add Formats Referenced in a Single SAS


Library to a CAS Session

Example
This example demonstrates how to use the %UDFSEL macro and the FORMAT
procedure to scan a single SAS library for references to user-defined formats, and
then to copy the formats that are being referenced to a SAS Cloud Analytic Services
format library. Unlike adding entire SAS format catalogs as shown in “Migrate User-
Defined Formats from SAS To CAS” on page 18, you can use this method to add
only the formats that you are using in your data. However, the %UDFSEL macro
scans data sets in only one library at a time. If your data sets are stored in multiple
libraries, you must perform the steps shown in this example for each library.
In this example, library Work contains one data set, Cars_Formatted. As shown in
the following output from the CONTENTS procedure, user-defined formats are
applied to columns EngineSize and Mpg_Highway.

Formats Enginsize and MPGrating exist in the Work.Formats catalog. The following
SAS program copies these formats to SAS Cloud Analytic Services format library
MyFmtLib.
/* options cashost="cloud.example.com" casport=5570; */
cas casauto; /* 1 */

%udfsel(work); /* 2 */

proc format library=work.formats cntlout=fmtdata; /* 3 */


%include selstmt/source2;
22 Chapter 2 / Using User-Defined Formats In SAS Cloud Analytic Services

run;

proc print data=work.fmtdata; /* 4 */


var FMTNAME START END LABEL;
run;

proc format cntlin=fmtdata casfmtlib='myFmtLib' /* 5 */


sessref=casauto;
run;

cas casauto listformats fmtlibname=myfmtlib members; /* 6 */


cas casauto listfmtranges fmtname=enginesize;
cas casauto listfmtranges fmtname=MPGrating;

1 The CAS statement starts session Casauto.


2 Macro %UDFSEL on page 6 scans the data sets in the Work library, and then
generates a SELECT statement for all of the user-defined formats that are
referenced. In this example, for simplicity, Work.Cars_Summary is the only data
set in the Work library. The SELECT statement is written to Selstmt, which is a
file reference to a temporary file. Here is the generated SELECT statement in
this example: select ENGINESIZE MPGRATING;
3 The FORMAT procedure uses the SELECT statement generated by the
%UDFSEL macro to get the format data from the Work.Formats catalog. The
%INCLUDE statement brings in the SELECT statement from file reference
Selstmt. The SOURCE2 option prints the SELECT statement to the SAS log.
The FORMAT procedure then stores the format data in table Work.Fmtdata.

Note: If your data uses formats in more than one format catalog, you can use a
CATNAME statement to combine the required format catalogs into one as was
done in “Migrate User-Defined Formats from SAS To CAS” on page 18.

4 Optional: The PRINT procedure prints columns FMTNAME, START, END, and
LABEL in table Fmtdata. This step enables you to verify that the format library
that is created in SAS Cloud Analytic Services is correct.
5 The FORMAT procedure reads the format data from table Fmtdata, and then
uses the data to create the formats in SAS Cloud Analytic Services format library
MyFmtLib.
6 Optional: The remaining CAS statements display the formats in format library
MyFmtLib and their ranges for verification. This step enables you to compare the
formats in MyFmtLib with the original format data. (See Output 2.1 on page 23
and “SAS Log” on page 23.)
Add Formats Referenced in a Single SAS Library to a CAS Session 23

Results
Output 2.1 Format Data in Table Fmtdata

SAS Log
Here is a partial listing of the program output that is written to the SAS log.
24 Chapter 2 / Using User-Defined Formats In SAS Cloud Analytic Services

1 /* options cashost="cloud.example.com" casport=5570; */


2 cas casauto;
...
3
4 %udfsel(work);
...
5
6 proc format library=work.formats cntlout=fmtdata;
7 %include selstmt/source2;
NOTE: %INCLUDE (level 1) file SELSTMT is file C:\Users\sasdemo\
AppData\Local\Temp\SAS Temporary Files\
_TD10804_d10a498_\#LN00037.
8 +select
9 +ENGINESIZE
10 +MPGRATING
11 +;
NOTE: %INCLUDE (level 1) ending.
12 run;
...
13
14 proc print data=work.fmtdata;
15 var FMTNAME START END LABEL;
16 run;
...
17
18 proc format cntlin=fmtdata casfmtlib='myFmtLib'
19 sessref=casauto;
20 run;
...
21
22 cas casauto listformats fmtlibname=myfmtlib
23 members;
NOTE: Fmtlib = MYFMTLIB
Scope = Session
Fmtsearch = YES
Format = enginesize
Format = mpgrating
NOTE: Request to LISTFORMAT completed for session CASAUTO.
24
25 cas casauto listfmtranges fmtname=enginesize;
Format Name Range
ENGINESIZE LOW-<2.7=Very economical
2.7-<4.1=Small
4.1-<5.5=Medium
5.5-<6.9=Large
6.9-HIGH=Very large

NOTE: Request to LISTFMTRANGES ENGINESIZE completed for


session CASAUTO.
26 cas casauto listfmtranges fmtname=MPGrating;
Format Name Range
MPGRATING LOW-<19=Poor
19-<24=Fair
24-<34=Good
34-HIGH=Excellent

NOTE: Request to LISTFMTRANGES MPGRATING completed for


session CASAUTO.
Promote a Format Library 25

Key Ideas
n Use the method demonstrated in this example to migrate only the SAS formats that
you are using in your data to SAS Cloud Analytic Services.
n Use the %UDFSEL on page 6 macro to generate a SELECT statement for the
formats that are referenced in the data sets in a single SAS library.
n Use the CATNAME statement to combine multiple format catalogs into one.

n Use the FORMAT procedure to move the formats to SAS Cloud Analytic Services
in two steps:

1 Get information about the selected formats from the format catalog, and
then store the information in a temporary SAS data set.

2 Copy the format data from the temporary SAS data set to a SAS Cloud
Analytic Services format library.

Promote a Format Library

Example
/* options cashost="cloud.example.com" casport=5570; */
cas casauto; /* 1 */

proc format lib=work.formats casfmtlib="myfmtlib"; /* 2 */


value abc
1 = 'yes'
2 = 'no'
3 = 'maybe'
;
run;

cas casauto promotefmtlib fmtlibname=myfmtlib replace; /* 3 */

1 The CAS statement starts session Casauto.


2 The FORMAT procedure is used with the CASFMTLIB= option to add a format
library named MyFmtLib to the active CAS session. The format library is added
with session scope.
3 The CAS statement PROMOTEFMTLIB option promotes the format library to
global scope. When format library MyFmtLib is promoted, it is made available to
all sessions that connect to the same server, but it is not automatically added to
their format search list. To use MyFmtLib in another session, it must be manually
26 Chapter 2 / Using User-Defined Formats In SAS Cloud Analytic Services

added to the session’s format search list. See “Add a Global Format Library to a
Session’s Format Search List” on page 27.

SAS Log
1 cas casauto;
...
2
3 proc format lib=work.formats casfmtlib="myfmtlib";
NOTE: Both CAS based formats and catalog-based formats will be written.
The CAS based formats will be written to the session CASAUTO.
4 value abc
5 1 = 'yes'
6 2 = 'no'
7 3 = 'maybe'
8 ;
NOTE: Format library MYFMTLIB added. Format search update using
parameter APPEND completed.
NOTE: Format ABC has been output.
9 run;
...
10
11 cas casauto promotefmtlib fmtlibname=myfmtlib replace;
NOTE: Request to PROMOTEFMTLIB MYFMTLIB completed for session CASAUTO.

Key Ideas
n When you promote a format library from a programming session, the format library
is made available to all sessions that connect to that CAS server.
n When a format library is promoted, it is not automatically added to the format
search list of the other sessions on that server. To use format library MyFmtLib in
another session, it must be added to the session’s format search list. See “Add a
Global Format Library to a Session’s Format Search List” on page 27.
n If you save your format library to a SASHDAT file, your SAS Cloud Analytic
Services system administrator can configure the server to automatically add your
format library to the format search list of all sessions on that server. See “Add and
Save a User-Defined Format” on page 6 and Configuration File Options.
Add a Global Format Library to a Session’s Format Search List 27

Add a Global Format Library to a Session’s


Format Search List

Example
This example assumes that global format libraries CompanyFormats and
DeptFormats are added automatically at session start-up, and that format library
MyFmtLib already exists in global scope. The following program inserts format
library MyFmtLib at the beginning of session Casauto’s format search list and
creates MyFmtLib in session scope.
/* options cashost="cloud.example.com" casport=5570; */
cas casauto; /* 1 */

cas casauto listfmtsearch; /* 2 */

cas casauto listformats scope=global; /* 3 */

cas casauto fmtsearch=(myfmtlib) position=insert; /* 4 */

cas casauto listfmtsearch; /* 5 */

1 The CAS statement starts session Casauto.


2 The CAS statement LISTFMTSEARCH option lists the current format search list
for session Casauto.
3 The CAS statement LISTFORMATS and SCOPE=GLOBAL options list the
global-scope format libraries that are available to the new session. The
MyFmtLib format library is listed, but it is not in the format search list for the new
session.
4 The CAS statement FMTSEARCH option inserts format library MyFmtLib at the
beginning of the format library search list. Format library MyFmtLib is added to
session Casauto in session scope.
5 The CAS statement LISTFMTSEARCH option lists the format library search list
for session Casauto. MyFmtLib is now at the beginning of the search list.

SAS Log
The SAS log for any session that runs the code to add the promoted format library
to the search list is similar to the following.
28 Chapter 2 / Using User-Defined Formats In SAS Cloud Analytic Services

1 cas casauto;
...
2
3 cas casauto listfmtsearch;
NOTE: FmtLibName = COMPANYFORMATS
Scope = Both
NOTE: FmtLibName = DEPTFORMATS
Scope = Both
NOTE: Request to LISTFMTSEARCH completed for session CASAUTO.
4
5 cas casauto listformats scope=global;
NOTE: Fmtlib = COMPANYFORMATS
Scope = Global
Fmtsearch = NA
NOTE: Fmtlib = DEPTFORMATS
Scope = Global
Fmtsearch = NA
NOTE: Fmtlib = MYFMTLIB
Scope = Global
Fmtsearch = NA
NOTE: Request to LISTFORMAT completed for session CASAUTO.
6
7 cas casauto fmtsearch=(myfmtlib) position=insert;
NOTE: Request to FMTSEARCH completed for session CASAUTO.
8
9 cas casauto listfmtsearch;
NOTE: FmtLibName = MYFMTLIB
Scope = Both
NOTE: FmtLibName = COMPANYFORMATS
Scope = Both
NOTE: FmtLibName = DEPTFORMATS
Scope = Both
NOTE: Request to LISTFMTSEARCH completed for session CASAUTO.

Key Ideas
n Use CAS statement options LISTFORMATS SCOPE=GLOBAL to list all of the
global format libraries that are available to the session.
n Use the FMTSEARCH= option to add one or more global format libraries to your
session’s format search list. Include the POSITION= option to specify whether the
new libraries are appended to the current path, are prepended to the current path,
or replace the current path.
n The global format libraries are added to your session in session scope.
Drop a Format Library from Session Scope 29

Drop a Format Library from Session Scope

Example
This example assumes that global format libraries CompanyFormats and
DeptFormats are added automatically at session start-up. The following program
drops format library DeptFormats from session scope and removes it from the
format search list in session Casauto.
/* options cashost="cloud.example.com" casport=5570; */
cas casauto; /* 1 */

cas casauto dropfmtlib fmtlibname=deptFormats /* 2 */


fmtsearchremove;

cas casauto listformats; /* 3 */

cas casauto listfmtsearch; /* 4 */

1 The CAS statement starts session Casauto.


2 The CAS statement DROPFMTLIB option drops format library DeptFormats from
session scope to remove its formats from session Casauto. The
FMTSEARCHREMOVE option removes DeptFormats from session Casauto’s
format search list. If not specified, DeptFormats remains in the format search list
even though its formats are no longer available in session scope.
3 The CAS statement LISTFORMATS option lists the format libraries in the active
session to verify that DeptFormats no longer exists in session scope.
4 The CAS statement LISTFMTSEARCH option lists the format-library search list
to verify that DeptFormats was removed from session Casauto’s format search
list.
30 Chapter 2 / Using User-Defined Formats In SAS Cloud Analytic Services

SAS Log
1 cas casauto;
...
2
3 cas casauto dropfmtlib fmtlibname=deptFormats
4 fmtsearchremove;
NOTE: The format library DEPTFORMATS was removed from the format search list.
NOTE: Request to DROPFMTLIB DEPTFORMATS completed for session CASAUTO.
5
6 cas casauto listformats;
NOTE: Fmtlib = COMPANYFORMATS
Scope = Session
Fmtsearch = YES
NOTE: Fmtlib = COMPANYFORMATS
Scope = Global
Fmtsearch = NA
NOTE: Fmtlib = DEPTFORMATS
Scope = Global
Fmtsearch = NA
NOTE: Request to LISTFORMAT completed for session CASAUTO.
7
8 cas casauto listfmtsearch;
NOTE: FmtLibName = COMPANYFORMATS
Scope = Both
NOTE: Request to LISTFMTSEARCH completed for session CASAUTO.

Key Ideas
n The format library must exist in the session’s format search list. Otherwise, an error
occurs.
n If the library exists in both global scope and session scope, the session-scope
library is dropped first. If you want to drop the global-scope format library as well,
issue the drop request again.
n By default, when you drop a format library, the format library name is not removed
from the session’s format search list. Use the REMOVEFMTSEARCH option in the
CAS statement to remove the dropped format library from the format search list.
31

3
Utility Macro for User-Defined
Formats

Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
%UDFSEL Autocall Macro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

Dictionary

%UDFSEL Autocall Macro


Generates a SELECT statement for the unique non-intrinsic user-defined formats that are referenced in the
data sets in a single SAS library.
Type: Autocall macro in SAS 9.4M5 and later releases
Applies to: User-defined formats in SAS Cloud Analytic Services
Restrictions: Autocall macros are included in a library supplied by SAS. This library might not be
installed at your site or might be a site-specific version. In SAS 9.4M5 and later releases,
if you cannot access this macro or if you want to find out if it is a site-specific version,
see your on-site SAS support personnel.
A SELECT statement can be generated for only one SAS library at a time.
Requirement: MAUTOSOURCE system option in SAS 9.4M5 and later releases
Tip: If you are using a release prior to SAS 9.4M5, the %UDFSEL macro is not available as
an autocall macro. In that case, you must compile the %UDFSEL macro before you can
use it. The %UDFSEL macro source code is listed in “%UDFSEL Macro Source Code”
on page 32.

Syntax
%UDFSEL (SAS-library-name);

Required Argument
SAS-library-name
specifies a single SAS library name.
32 Chapter 3 / Utility Macro for User-Defined Formats

Details
About the %UDFSEL Macro
The %UDFSEL macro scans the data sets in library SAS-library-name and
generates a SELECT statement for all of the unique non-intrinsic formats that are
referenced. The generated SELECT statement is written to Selstmt, which is a file
reference to a temporary file. You can then use the generated SELECT statement
with the FORMAT procedure to get information about the formats from a specified
format catalog and store the information in a SAS data set. After you have stored
the format data in a SAS data set, you can use the format data set with the
FORMAT procedure to copy the selected formats from SAS to a SAS Cloud Analytic
Services format library.

%UDFSEL Macro Source Code


If you are using SAS 9.4M5 or a later release, the %UDFSEL macro is already
defined as an autocall macro. Otherwise, you must compile the %UDFSEL macro
source code before you can use it. Here is the source code for the %UDFSEL
macro. To compile the macro, copy the source code, paste it into SAS Studio, and
then run it.
/****************************************************************/
/* S A S A U T O C A L L L I B R A R Y */
/* */
/* NAME: udfsel */
/* TITLE: Create SELECT statement of user-defined formats */
/* PRODUCT: BASE */
/* SYSTEM: ALL */
/* */
/* DESCRIPTION: Will create a SELECT statement of all UDFs */
/* referenced in all datasets for a given libref. */
/* */
/****************************************************************/
/* USAGE: %udfsel(libref); */
/* where libref is a defined libref. A fileref called SELSTMT */
/* will be defined to a temporary file, and will contain a */
/* SELECT ... ; statement. This SELECT statement can be used by */
/* PROC FORMAT or FMTC2ITM. Note that if there are no UDFs in */
/* any data set, then SELECT DATE; is generated. The DATE format*/
/* is not a UDF and will therefore never be found to be */
/* selected. SELECT; is invalid syntax, and omitting the SELECT */
/* statement altogether means all formats would be selected. */
/****************************************************************/

%macro udfsel(libref);

%local contents_output intrinsic_output;

*-----first get all format info for all data sets-----*;


proc contents data=&libref.._all_
out=_data_(keep=format rename=(format=fmtname)) noprint; run;

*-----save the name of the generated data set-----*;


%let contents_output=%sysfunc(getoption(_last_));

*-----obtain only the unique list of format names-----*;


%UDFSEL Autocall Macro 33

proc sort nodupkey; by fmtname; run;

*-----find all intrinsic formats from format enumeration-----*;

/* Note that this step make take a while due to the use of sashelp.vformat. */

data _data_(keep=fmtname); set sashelp.vformat(where=(libname=' ' and fmttype='F')); run;

*-----save the name of the generated data set-----*;


%let intrinsic_output=%sysfunc(getoption(_last_));

*-----obtain only the unique list of format names-----*;


proc sort nodupkey; by fmtname; run;

*-----create the fileref for the temporary file-----*;


filename selstmt temp;

*-----merge to determine what formats are not intrinsic and generate the SELECT statement-----*;
data _null_; merge &intrinsic_output.(in=is_intrinsic) &contents_output.(in=want) end=eof; by fmtname
file selstmt;
retain n_written 0;
if _n_=1 then put 'select';
if fmtname ne ' ' and want and not is_intrinsic then do;
n_written+1;
put fmtname;
end;
if eof then do;
if n_written = 0
then put 'date';
put ';';
end;
run;

*-----housekeeping: delete our generated data sets-----*;


proc delete data=&intrinsic_output.; run;
proc delete data=&contents_output.; run;

%mend udfsel;

Example
%udfsel(work); /* 1 */

proc format library=work.formats cntlout=fmtdata; /* 2 */


%include selstmt/source2;
run;

1 Macro %UDFSEL scans the data sets in the Work library, and then generates a
SELECT statement for all of the user-defined formats that are referenced. In this
example, Work.Cars_Summary is the only data set in the Work library. The
SELECT statement is written to file reference Selstmt. Here is a simple example
of the SELECT statement: select ENGINESIZE MPGRATING;
2 The FORMAT procedure uses the SELECT statement generated by the
%UDFSEL macro to get the format data from the Work.Formats catalog. The
%INCLUDE statement brings in the SELECT statement from file reference
34 Chapter 3 / Utility Macro for User-Defined Formats

Selstmt. The SOURCE2 option prints the SELECT statement to the SAS log.
The FORMAT procedure then stores the format data in table Work.Fmtdata.

Note: If your data uses formats in more than one format catalog, you can use a
CATNAME statement to combine the required format catalogs into one. Here is
an example that combines format catalogs Work.Formats and Orion.Mailfmt into
Work.Mycat.
catname work.mycat(work.formats orion.mailfmt);

See Also
“Add Formats Referenced in a Single SAS Library to a CAS Session” on page 21

You might also like