Published on :
Data Management CREATION_INTERNE

Permanent Formats Provided by SAS

This code is also available in: Deutsch Español Français
Awaiting validation
In addition to user-defined formats, SAS© offers a variety of predefined formats that can be applied to data within the SAS© Cloud Analytic Services (CAS) environment. These formats allow control over how data values are read, stored, or displayed, thereby improving the readability and interpretation of results. Compatibility with CAS ensures that these formats can be used with in-memory distributed tables, facilitating analysis and reporting on large datasets.
Data Analysis

Type : CREATION_INTERNE


Examples use generated data (datalines) to ensure their autonomy and reproducibility.

1 Code Block
DATA STEP Data
Explanation :
This simple example creates a SAS table 'commandes' and applies the DATE9. format to the 'Date_Commande' variable to display dates in YYYYMMDD format. Data is created directly in the DATA step for reproducibility.
Copied!
1DATA work.commandes;
2 INPUT ID_Commande Date_Commande:MMDDYY10. Montant;
3 FORMAT Date_Commande DATE9.;
4 DATALINES;
5 101 01/15/2023 150.75
6 102 02/20/2023 200.00
7 103 03/10/2023 50.25
8 104 04/05/2023 300.50
9 ;
10RUN;
11 
12PROC PRINT DATA=work.commandes; TITLE 'Commandes avec format de date basique'; RUN;
2 Code Block
DATA STEP / PROC PRINT Data
Explanation :
This second example illustrates the use of currency formats (DOLLARx.y) and thousands separators (COMMAx.) to improve the presentation of financial data and quantities. It also calculates a 'Prix_Total' in a DATA step.
Copied!
1DATA work.produits;
2 INPUT Code_Produit $ Prix_Unitaire Quantite;
3 Prix_Total = Prix_Unitaire * Quantite;
4 FORMAT Prix_Unitaire DOLLAR8.2 Prix_Total DOLLAR10.2 Quantite COMMA8.;
5 DATALINES;
6 A101 12.99 1000
7 B202 5.50 2500
8 C303 25.00 500
9 D404 1.25 10000
10 ;
11RUN;
12 
13PROC PRINT DATA=work.produits; TITLE 'Produits avec formats monétaires et décimaux'; RUN;
3 Code Block
DATA STEP / PROC FORMAT / PROC CAS Data
Explanation :
This example combines the creation of a user-defined format (STATFMT.) with the use of SAS Cloud Analytic Services (CAS). It first loads client data into a CAS table, then applies the custom format to the 'Statut_Anciennete' variable. A new 'Cat_CA' variable is created based on turnover, demonstrating conditional logic and the application of another format. The formatted table is promoted to CAS, making the formats available for distributed analyses.
Copied!
1LIBNAME mycas CAS; /* Assurez-vous d'avoir une session CAS active */
2 
3DATA work.clients_temp;
4 INPUT ID_Client $ Statut_Anciennete $ Chiffre_Affaires;
5 DATALINES;
6 C001 Gold 15000
7 C002 Silver 5000
8 C003 Bronze 1000
9 C004 Gold 25000
10 C005 Silver 7500
11 ;
12RUN;
13 
14/* Charger la table dans CAS */
15PROC CASUTIL SESSREF=mycas;
16 LOAD DATA=work.clients_temp OUTFATABLE=mycas.clients_cas REPLACE;
17RUN;
18 
19/* Définir un format personnalisé */
20PROC FORMAT;
21 VALUE $STATFMT
22 'Gold'='Client Premium'
23 'Silver'='Client Fidèle'
24 'Bronze'='Nouveau Client'
25 OTHER = 'Non classifié';
26RUN;
27 
28DATA mycas.clients_formated_cas(promote=yes); /* Créer une nouvelle table CAS avec le format appliqué */
29 SET mycas.clients_cas;
30 FORMAT Statut_Anciennete $STATFMT.;
31 IF Chiffre_Affaires >= 10000 THEN Cat_CA = 'Élevé';
32 ELSE IF Chiffre_Affaires >= 5000 THEN Cat_CA = 'Moyen';
33 ELSE Cat_CA = 'Faible';
34 FORMAT Cat_CA $10.;
35RUN;
36 
37PROC CAS SESSREF=mycas;
38 TABLE.FETCH / TABLE={NAME='clients_formated_cas'};
39RUN;
4 Code Block
DATA STEP / PROC CAS Data
Explanation :
This example highlights the integration of formats into a Viya/CAS workflow for numerical data. It uses complex formats like DATEAMPMx. and COMMAXx.y for visualization. Furthermore, it simulates a resource-intensive calculation ('Valeur_Calculee') that would benefit from the distributed computing power of CAS, and applies a EUROx.y monetary format to the result. The table is then promoted and its contents are displayed via a CAS action, confirming the application of formats in a distributed environment.
Copied!
1LIBNAME mycas CAS; /* Assurez-vous d'avoir une session CAS active */
2 
3DATA work.performances_temp;
4 INPUT Identifiant Date_Mesure:DATE9. Valeur_Brute;
5 DATALINES;
6 S1 01JAN2023 1234567.89
7 S2 15MAR2023 98765.432
8 S3 01JUN2023 123.456
9 S4 20SEP2023 12345.6789
10 ;
11RUN;
12 
13/* Charger la table dans CAS */
14PROC CASUTIL SESSREF=mycas;
15 LOAD DATA=work.performances_temp OUTFATABLE=mycas.performances_cas REPLACE;
16RUN;
17 
18/* Appliquer des formats complexes pour l'affichage dans CAS */
19DATA mycas.performances_formated_cas(promote=yes);
20 SET mycas.performances_cas;
21 FORMAT Date_Mesure DATEAMPM19. Valeur_Brute COMMAX12.4;
22 /* Simulation d'une opération complexe pouvant bénéficier de CAS */
23 Valeur_Calculee = Valeur_Brute * 1.05 / 0.95;
24 FORMAT Valeur_Calculee EURO12.2;
25RUN;
26 
27PROC CAS SESSREF=mycas;
28 TABLE.CONTENTS / TABLE={NAME='performances_formated_cas'};
29 TABLE.FETCH / TABLE={NAME='performances_formated_cas'};
30RUN;
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 © SAS Institute Inc. All Rights Reserved.