When you specify a CAS engine libref on the output dataset in a DATA step without input data, the DATA step executes in CAS. The CAS DATA step only operates on in-memory CAS tables, meaning both input and output data must be in-memory CAS tables. CAS tables must contain at least one variable. The DATA step automatically executes in CAS when you use CAS engine librefs on both input and output datasets, or when you do not specify an input dataset and specify output data using a CAS engine libref. Not all language elements are supported in a CAS DATA step.
Data Analysis
Type : CREATION_INTERNE
Examples use generated data (datalines) or dynamically created data.
1 Code Block
DATA Step / PROC PRINT Data
Explanation : This basic example shows how to create a CAS table 'produits_cas' from inline data, then convert it into a local SAS dataset 'mysas.produits_sas'. A 'mysas' libref is created to specify the save path for the SAS dataset. Finally, a PROC PRINT is used to display the content of the new SAS table. The temporary CAS table is then dropped.
Copied!
libname mycas cas;
data mycas.produits_cas;
input ID $ NomProduit $ Prix;
datalines;
A001 Pomme 1.00
B002 Poire 1.20
C003 Orange 0.90
;
run;
libname mysas '/tmp';
data mysas.produits_sas;
set mycas.produits_cas;
run;
proc print data=mysas.produits_sas;
title 'Produits convertis en SAS Dataset';
run;
/* Nettoyage */
proc casutil;
droptable casdata='produits_cas' incaslib='CASUSER';
run;
Explanation : This intermediate example shows how to convert a CAS table to a SAS dataset, applying common options. We create a CAS table 'ventes_cas'. During conversion, only sales with a 'Quantite' greater than 8 are selected ('where') and the 'IDVente' variable is dropped ('drop=IDVente'). Additionally, a new 'TotalVente' variable is calculated. The resulting SAS table 'mysas.ventes_filtrees' contains only the specified records and variables, plus the new calculated variable. The temporary CAS table is then dropped.
Copied!
libname mycas cas;
data mycas.ventes_cas;
input IDVente $ Produit $ Quantite PrixUnitaire DateVente :yymmdd10.;
format DateVente yymmdd10.;
datalines;
V001 Pomme 10 1.00 2023-01-05
V002 Poire 5 1.20 2023-01-05
V003 Orange 12 0.90 2023-01-06
V004 Pomme 7 1.00 2023-01-06
V005 Banane 20 0.75 2023-01-07
;
run;
libname mysas '/tmp';
data mysas.ventes_filtrees (drop=IDVente);
set mycas.ventes_cas;
where Quantite > 8;
TotalVente = Quantite * PrixUnitaire;
run;
proc print data=mysas.ventes_filtrees;
title 'Ventes filtrées et calculées, converties en SAS Dataset';
run;
/* Nettoyage */
proc casutil;
droptable casdata='ventes_cas' incaslib='CASUSER';
run;
1
LIBNAME mycas cas;
2
3
DATA mycas.ventes_cas;
4
INPUT IDVente $ Produit $ Quantite PrixUnitaire DateVente :yymmdd10.;
5
FORMAT DateVente yymmdd10.;
6
DATALINES;
7
V001 Pomme 101.002023-01-05
8
V002 Poire 51.202023-01-05
9
V003 Orange 120.902023-01-06
10
V004 Pomme 71.002023-01-06
11
V005 Banane 200.752023-01-07
12
;
13
RUN;
14
15
LIBNAME mysas '/tmp';
16
17
DATA mysas.ventes_filtrees (drop=IDVente);
18
SET mycas.ventes_cas;
19
where Quantite > 8;
20
TotalVente = Quantite * PrixUnitaire;
21
RUN;
22
23
PROC PRINTDATA=mysas.ventes_filtrees;
24
title 'Ventes filtrées et calculées, converties en SAS Dataset';
Explanation : This advanced example uses a macro variable to filter data and applies a custom format during conversion. A CAS table 'employes_cas' is created. A custom format 'service_fmt' is defined and applied to the 'Service' variable. A macro variable `min_salaire` is used in the WHERE clause to select employees with a salary greater than or equal to a defined value. A new 'CategorieAge' variable is also created based on the birth year. The resulting SAS table 'mysas.employes_filtres_cas' contains the filtered data, the new variable, and the applied format. The temporary CAS tables and format are then dropped.
Copied!
libname mycas cas;
/* Création d'une table CAS avec des données */
data mycas.employes_cas;
input ID Employe $ Service $ Salaire Anniversaire :yymmdd10.;
format Anniversaire yymmdd10.;
datalines;
101 Dupont RH 50000 1980-05-15
102 Martin Ventes 65000 1975-11-22
103 Dubois RH 52000 1992-03-01
104 Lefevre Marketing 60000 1988-07-30
;
run;
/* Création d'un format personnalisé dans CAS */
proc format library=mycas.formats_lib;
value $service_fmt
'RH' = 'Ressources Humaines'
'Ventes' = 'Commercial'
'Marketing' = 'Marketing & Com';
run;
libname mysas '/tmp';
%let min_salaire = 55000;
data mysas.employes_filtres_cas;
set mycas.employes_cas;
where Salaire >= &min_salaire;
length CategorieAge $15.;
if year(Anniversaire) <= 1980 then CategorieAge = 'Senior';
else CategorieAge = 'Junior';
format Service $service_fmt.;
run;
proc print data=mysas.employes_filtres_cas;
title 'Employés filtrés et formatés (SAS Dataset)';
run;
/* Nettoyage */
proc casutil;
droptable casdata='employes_cas' incaslib='CASUSER';
run;
proc format library=mycas.formats_lib;
delete $service_fmt;
run;
1
LIBNAME mycas cas;
2
3
/* Création d'une table CAS avec des données */
4
DATA mycas.employes_cas;
5
INPUT ID Employe $ Service $ Salaire Anniversaire :yymmdd10.;
6
FORMAT Anniversaire yymmdd10.;
7
DATALINES;
8
101 Dupont RH 500001980-05-15
9
102 Martin Ventes 650001975-11-22
10
103 Dubois RH 520001992-03-01
11
104 Lefevre Marketing 600001988-07-30
12
;
13
RUN;
14
15
/* Création d'un format personnalisé dans CAS */
16
PROC FORMAT library=mycas.formats_lib;
17
value $service_fmt
18
'RH' = 'Ressources Humaines'
19
'Ventes' = 'Commercial'
20
'Marketing' = 'Marketing & Com';
21
RUN;
22
23
LIBNAME mysas '/tmp';
24
25
%let min_salaire = 55000;
26
27
DATA mysas.employes_filtres_cas;
28
SET mycas.employes_cas;
29
where Salaire >= &min_salaire;
30
LENGTH CategorieAge $15.;
31
IF year(Anniversaire) <= 1980THEN CategorieAge = 'Senior';
32
ELSE CategorieAge = 'Junior';
33
FORMAT Service $service_fmt.;
34
RUN;
35
36
PROC PRINTDATA=mysas.employes_filtres_cas;
37
title 'Employés filtrés et formatés (SAS Dataset)';
Explanation : This Viya/CAS-focused example demonstrates how to handle advanced cases when converting a CAS table to a SAS dataset, including missing value management and logging. A CAS table 'transactions_cas' is created with missing amounts. The conversion DATA step includes logic to replace missing amounts with 0. It also uses a condition to simulate invalid data detection (although the `call cas.log` part is commented out to avoid an actual error in this example, it illustrates the error handling approach). The objective is to ensure conversion robustness and optimize the data process in a distributed environment. The temporary CAS table is then dropped.
Copied!
libname mycas cas;
/* Création d'une table CAS avec des données et des valeurs manquantes */
data mycas.transactions_cas;
input IDTrans $ Montant DateTrans :yymmdd10. Statut $;
format DateTrans yymmdd10.;
datalines;
T001 100.50 2023-10-01 Succes
T002 . 2023-10-02 Echec
T003 250.00 2023-10-03 Succes
T004 50.25 2023-10-04 Succes
T005 . 2023-10-05 Annule
;
run;
libname mysas '/tmp';
/* Utilisation de l'option _ERROR_ pour la gestion d'erreurs et de VALIDFMT */
data mysas.transactions_processed;
set mycas.transactions_cas;
/* Gérer les montants manquants - les remplacer par 0 */
if missing(Montant) then Montant = 0;
/* Simuler une erreur de format pour illustrer _ERROR_ (non exécutée dans cet exemple, mais pour démonstration) */
/* if IDTrans = 'T002' then call symputx('invalid_data_found', 'YES'); */
/* Utilisation de l'option VALIDFMT pour vérifier les formats des variables */
/* L'option VALIDFMT est généralement appliquée lors de la lecture, ici on s'assure de la propreté des données */
if Statut not in ('Succes', 'Echec', 'Annule') then call cas.log('Statut invalide détecté pour IDTrans: ' || IDTrans, 'ERROR');
run;
proc print data=mysas.transactions_processed;
title 'Transactions traitées et converties (SAS Dataset)';
run;
/* Nettoyage */
proc casutil;
droptable casdata='transactions_cas' incaslib='CASUSER';
run;
1
LIBNAME mycas cas;
2
3
/* Création d'une table CAS avec des données et des valeurs manquantes */
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.