We’re smarter together. Learn from this collection of community knowledge and add your expertise.

Working with User-Defined Formats with SAS Cloud Analytic Services

by SAS Employee UttamKumar ‎05-26-2017 10:31 AM - edited ‎05-31-2017 02:33 PM (997 Views)

SAS provides formats for controlling how variables are printed. In the SAS Cloud Analytic Services (CAS) environment, if you are analyzing data using SAS formats, you must assign the format to the variable before the data table is loaded to CAS. You can use PROC FORMAT in your SAS programs to create user-defined formats and to add formats and format libraries for a CAS session.

 

A format library can be temporary or session-scoped for the duration of your programming session. Alternatively, it can be set to have global scope for the duration of your CAS server session. You can persist the user-defined formats and CAS format library in a SASHDAT file.Similarly, you can add or load formats to your CAS session from a persisted SASHDAT file.  

 

 

The following code demonstrates the creation of a format and format library for a CAS session.

 

proc format casfmtlib="casformats";

   value 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“ ;    

 

 

 

The following code demonstrates the persistence of a CAS format in a .sashdat table.

 

cas mySession savefmtlib fmtlibname=casformats

   table=enginefmt replace;   UDF_CAS_1.png    

 

 

The following code demonstrates a data table load to CAS with user-defined formats. For example, the user-defined format “enginesize” is attached to the “enginesize” variable.

 

proc casutil;

   format enginesize enginesize.;

   load data=sashelp.cars casout="cars" replace;

   contents casdata="cars";

quit;

 

 

  UDF_CAS_2.png  

 

 

 

A CAS format library is used within the CAS server and the formats must be assigned to the library when used in a DATA step or PROCS to execute on CAS. This applies to procedures such as PROC CARDINALITY, PROC MDSUMMARY, PROC BINNING, and others.  

 

The following code demonstrates the execution of PROC MDSUMMARY in CAS using user-defined formats.

 

proc mdsummary data=casuser.cars;

   var mpg_highway;

   groupby enginesize / out=casuser.mpg_hwy_by_size;

run;   UDF_CAS_3.png

 

 

You can add ‘locale’ based user-defined formats to the CAS format library to use in a ‘locale’ based data analyzing process. The following code demonstrates the creation of a ‘locale’ based user-defined formats.

 

cas mySession sessopts=(caslib=casuser timeout=1800 locale="en_US" );

 

options locale=en_US;

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

   value abc

      1='one'

      2='two'

      3='three';

run;

 

options locale=fr_FR;

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

   value abc

     1='un'

     2='deux'

     3='trois';

run;  

 

 

 

The following code and log show the list of CAS format libraries and their members. In this example, notice there are two members in the same CAS format library (Fmtlib).

 

57 cas mySession listformats members;

NOTE: Fmtlib = CASFORMATS

Scope = Session

Fmtsearch = YES

Format = fr_fr-abc

Format = en_us-abc

NOTE: Request to LISTFORMAT completed for session MYSESSION.

58  

 

 

 

The following code and log show the retrieval of different values from user-defined format ranges when the options for a CAS session change from one ‘locale’ to another. In this example, notice when the CAS session option is locale=’en_US’, the range value is displayed in English. Whereas, when the CAS session option is locale=’fr_FR’, the range value is displayed in French.

 

57 /* Confirm that the format is available in each locale. */

58 cas mySession sessopts=(locale="en_US");

NOTE: The CAS statement request to update one or more session options for session MYSESSION completed.

59 cas mySession listfmtranges fmtname=abc;

NOTE: Format Name = ABC

Range = 1=one

NOTE: Format Name =

Range = 2=two

NOTE: Format Name =

Range = 3=three

NOTE: Request to LISTFMTRANGES ABC completed for session MYSESSION.

60

 

61 cas mySession sessopts=(locale="fr_FR");

NOTE: The CAS statement request to update one or more session options for session MYSESSION completed.

62 cas mySession listfmtranges fmtname=abc;

NOTE: Format Name = ABC

Range = 1=un

NOTE: Format Name =

Range = 2=deux

NOTE: Format Name =

Range = 3=trois

NOTE: Request to LISTFMTRANGES ABC completed for session MYSESSION.

63  

 

 

 

In an active CAS session, you can list the available CAS format libraries and search paths by using the following CAS action options.

  • The LISTFORMATS option to list the format libraries available to the CAS session.
  • The LISTFMTSRANGES option used with FMTNAME= to print the format values.
  • The LISTFMTSEARCH option to display the format libraries in the search path.

 

As a CAS administrator, you can configure the CAS server to automatically add user-defined formats and include them in the search path for the CAS session when the user initiates a connection. Add cas.fmtcaslib= and cas.addfmtlib= option in the CAS server configuration file to automatically include user-defined formats to each user’s search path.  

 

In addition to user-defined formats, SAS also provides permanent formats that can be used with SAS Cloud Analytic Services. For a list of formats that are supported in CAS, see the SAS® Viya™ Formats and Informats Reference.  

 

 

For more information about using user-defined formats in CAS, see:

Working with user-defined formats in CAS  

Contributors
Your turn
Sign In!

Want to write an article? Sign in with your profile.


Looking for the Ask the Expert series? Find it in its new home: communities.sas.com/askexpert.