Examples use generated data (datalines) or dynamically created tables.
1 Code Block
PROC CAS
Explanation : This simplest example shows how to use the SOURCE statement to assign a multi-line text block to a CASL variable named 'maVariableTexte'. The text between SOURCE and ENDSOURCE is captured literally, including line breaks. The 'print' statement is then used to display the content of this variable in the SAS log.
Copied!
proc cas;
/* Déclare une variable CASL pour stocker le texte */
source maVariableTexte;
Ceci est la première ligne.
Ceci est la deuxième ligne.
Et voici la troisième ligne.
endsource;
/* Affiche le contenu de la variable */
print maVariableTexte;
run;
quit;
1
PROC CAS;
2
/* Déclare une variable CASL pour stocker le texte */
3
SOURCE maVariableTexte;
4
Ceci est la première ligne.
5
Ceci est la deuxième ligne.
6
Et voici la troisième ligne.
7
ENDSOURCE;
8
9
/* Affiche le contenu de la variable */
10
PRINT maVariableTexte;
11
RUN;
12
QUIT;
2 Code Block
PROC CAS / DATA STEP Data
Explanation : This example demonstrates how to encapsulate a complete SAS code block (here, a PROC PRINT) into a CASL variable using the SOURCE statement. After creating a CAS table (casuser.produits) for demonstration, the content of the 'codeSasDynamique' variable is executed in the CAS session using the 'cas cascode / code=' statement. This allows for dynamic execution of SAS code that is generated or programmatically stored.
Copied!
proc cas;
/* Crée une table CAS pour l'exemple */
data casuser.produits;
input ID Produit $ Prix;
datalines;
1 Pomme 1.00
2 Orange 0.75
3 Banane 0.50
4 Poire 1.20
;
run;
/* Encapsule un bloc de code SAS dans une variable CASL */
source codeSasDynamique;
proc print data=casuser.produits;
var ID Produit Prix;
title 'Liste des Produits';
run;
endsource;
/* Exécute le code SAS stocké dans la variable */
cas cascode / code=codeSasDynamique;
run;
quit;
1
PROC CAS;
2
/* Crée une table CAS pour l'exemple */
3
DATA casuser.produits;
4
INPUT ID Produit $ Prix;
5
DATALINES;
6
1 Pomme 1.00
7
2 Orange 0.75
8
3 Banane 0.50
9
4 Poire 1.20
10
;
11
RUN;
12
13
/* Encapsule un bloc de code SAS dans une variable CASL */
14
SOURCE codeSasDynamique;
15
PROC PRINTDATA=casuser.produits;
16
var ID Produit Prix;
17
title 'Liste des Produits';
18
RUN;
19
ENDSOURCE;
20
21
/* Exécute le code SAS stocké dans la variable */
22
cas cascode / code=codeSasDynamique;
23
RUN;
24
QUIT;
3 Code Block
PROC CAS
Explanation : This advanced example shows two uses: first, the dynamic construction of a CASL action call. A SAS macro variable ('filtreRegion') is used to parameterize the WHERE clause of a 'builtins.filter' action. The 'actionCaslDynamique' variable contains the CASL code which is then executed by 'evaluate'. Second, it illustrates the definition of a complete CASL function within a SOURCE block and its execution, demonstrating flexibility for functional programming in CASL.
Copied!
proc cas;
/* Crée une table CAS simple */
data casuser.ventes;
input Region $ Ventes;
datalines;
Nord 100
Sud 150
Est 120
Ouest 180
;
run;
/* Définit une macro-variable pour un filtre */
%let filtreRegion = 'Nord';
/* Construit dynamiquement un appel d'action CASL avec une macro-variable */
source actionCaslDynamique;
builtins.filter(table={name='ventes', caslib='casuser'},
where='Region = &filtreRegion.');
endsource;
/* Affiche et exécute le code CASL généré */
print 'Code CASL généré: ' actionCaslDynamique;
evaluate actionCaslDynamique;
/* Autre exemple : définition et appel d'une fonction CASL */
source maFonctionCasl;
function maFonction(msg);
print 'Message de la fonction : ' || msg;
return msg || ' Traité';
endfunction;
endsource;
evaluate maFonctionCasl;
result = maFonction('Bonjour du CAS');
print 'Résultat de la fonction : ' result;
run;
quit;
1
PROC CAS;
2
/* Crée une table CAS simple */
3
DATA casuser.ventes;
4
INPUT Region $ Ventes;
5
DATALINES;
6
Nord 100
7
Sud 150
8
Est 120
9
Ouest 180
10
;
11
RUN;
12
13
/* Définit une macro-variable pour un filtre */
14
%let filtreRegion = 'Nord';
15
16
/* Construit dynamiquement un appel d'action CASL avec une macro-variable */
/* Autre exemple : définition et appel d'une fonction CASL */
27
SOURCE maFonctionCasl;
28
function maFonction(msg);
29
PRINT'Message de la fonction : ' || msg;
30
return msg || ' Traité';
31
endfunction;
32
ENDSOURCE;
33
34
evaluate maFonctionCasl;
35
RESULT = maFonction('Bonjour du CAS');
36
PRINT'Résultat de la fonction : 'RESULT;
37
RUN;
38
QUIT;
4 Code Block
PROC CAS Data
Explanation : This example illustrates the power of the SOURCE statement in a Viya/CAS environment for flow and error management. The first scenario uses a macro variable to conditionally control the creation of a CAS table via a `builtins.loadtable` block. The second scenario shows how to integrate CASL `TRY-CATCH` blocks directly into a SOURCE code to handle potential errors during the execution of CAS actions (here, attempting to drop a non-existent table). These techniques are fundamental for the robustness and adaptability of SAS/CASL programs in production.
Copied!
proc cas;
/* Crée une table source */
data casuser.donnees_source;
input Valeur;
datalines;
10
20
30
;
run;
/* Scénario 1 : Création de table conditionnelle */
%let creerTable = 1;
source creationTableConditionnelle;
if &creerTable. = 1 then do;
builtins.loadtable(path='donnees_source.sashdat', caslib='casuser', casout={name='donnees_chargees', replace=true});
print 'Table donnees_chargees créée avec succès.';
end;
else do;
print 'Création de table ignorée.';
end;
endsource;
evaluate creationTableConditionnelle;
/* Scénario 2 : Exécution d'une action avec gestion d'erreur simulée */
source testActionAvecErreur;
try {
builtins.droptable(name='table_non_existante', caslib='casuser', quiet=false);
print 'Cette ligne ne devrait pas s''afficher.';
} catch (e) {
print 'Erreur attrapée : ' || e.message;
}
endsource;
evaluate testActionAvecErreur;
/* Nettoyage */
builtins.droptable(name='donnees_source', caslib='casuser', quiet=true);
builtins.droptable(name='donnees_chargees', caslib='casuser', quiet=true);
run;
quit;
1
PROC CAS;
2
/* Crée une table source */
3
DATA casuser.donnees_source;
4
INPUT Valeur;
5
DATALINES;
6
10
7
20
8
30
9
;
10
RUN;
11
12
/* Scénario 1 : Création de table conditionnelle */
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.