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!
DATA work.commandes;
INPUT ID_Commande Date_Commande:MMDDYY10. Montant;
FORMAT Date_Commande DATE9.;
DATALINES;
101 01/15/2023 150.75
102 02/20/2023 200.00
103 03/10/2023 50.25
104 04/05/2023 300.50
;
RUN;
PROC PRINT DATA=work.commandes; TITLE 'Commandes avec format de date basique'; RUN;
PROC PRINTDATA=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!
DATA work.produits;
INPUT Code_Produit $ Prix_Unitaire Quantite;
Prix_Total = Prix_Unitaire * Quantite;
FORMAT Prix_Unitaire DOLLAR8.2 Prix_Total DOLLAR10.2 Quantite COMMA8.;
DATALINES;
A101 12.99 1000
B202 5.50 2500
C303 25.00 500
D404 1.25 10000
;
RUN;
PROC PRINT DATA=work.produits; TITLE 'Produits avec formats monétaires et décimaux'; RUN;
1
DATA 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.991000
7
B202 5.502500
8
C303 25.00500
9
D404 1.2510000
10
;
11
RUN;
12
13
PROC PRINTDATA=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!
LIBNAME mycas CAS; /* Assurez-vous d'avoir une session CAS active */
DATA work.clients_temp;
INPUT ID_Client $ Statut_Anciennete $ Chiffre_Affaires;
DATALINES;
C001 Gold 15000
C002 Silver 5000
C003 Bronze 1000
C004 Gold 25000
C005 Silver 7500
;
RUN;
/* Charger la table dans CAS */
PROC CASUTIL SESSREF=mycas;
LOAD DATA=work.clients_temp OUTFATABLE=mycas.clients_cas REPLACE;
RUN;
/* Définir un format personnalisé */
PROC FORMAT;
VALUE $STATFMT
'Gold'='Client Premium'
'Silver'='Client Fidèle'
'Bronze'='Nouveau Client'
OTHER = 'Non classifié';
RUN;
DATA mycas.clients_formated_cas(promote=yes); /* Créer une nouvelle table CAS avec le format appliqué */
SET mycas.clients_cas;
FORMAT Statut_Anciennete $STATFMT.;
IF Chiffre_Affaires >= 10000 THEN Cat_CA = 'Élevé';
ELSE IF Chiffre_Affaires >= 5000 THEN Cat_CA = 'Moyen';
ELSE Cat_CA = 'Faible';
FORMAT Cat_CA $10.;
RUN;
PROC CAS SESSREF=mycas;
TABLE.FETCH / TABLE={NAME='clients_formated_cas'};
RUN;
1
LIBNAME mycas CAS; /* Assurez-vous d'avoir une session CAS active */
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!
LIBNAME mycas CAS; /* Assurez-vous d'avoir une session CAS active */
DATA work.performances_temp;
INPUT Identifiant Date_Mesure:DATE9. Valeur_Brute;
DATALINES;
S1 01JAN2023 1234567.89
S2 15MAR2023 98765.432
S3 01JUN2023 123.456
S4 20SEP2023 12345.6789
;
RUN;
/* Charger la table dans CAS */
PROC CASUTIL SESSREF=mycas;
LOAD DATA=work.performances_temp OUTFATABLE=mycas.performances_cas REPLACE;
RUN;
/* Appliquer des formats complexes pour l'affichage dans CAS */
DATA mycas.performances_formated_cas(promote=yes);
SET mycas.performances_cas;
FORMAT Date_Mesure DATEAMPM19. Valeur_Brute COMMAX12.4;
/* Simulation d'une opération complexe pouvant bénéficier de CAS */
Valeur_Calculee = Valeur_Brute * 1.05 / 0.95;
FORMAT Valeur_Calculee EURO12.2;
RUN;
PROC CAS SESSREF=mycas;
TABLE.CONTENTS / TABLE={NAME='performances_formated_cas'};
TABLE.FETCH / TABLE={NAME='performances_formated_cas'};
RUN;
1
LIBNAME mycas CAS; /* Assurez-vous d'avoir une session CAS active */
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.
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.