Published on :
CASL CREATION_INTERNE

PROC CAS SOURCE Statement

This code is also available in: Deutsch Español Français
Awaiting validation
The SOURCE statement is used in the PROC CAS environment to define a CASL variable whose value is a multi-line text block. This text block can be any string of characters, including SAS© or CASL code. The text is delimited by the keywords `SOURCE` (followed by the variable name) and `ENDSOURCE`. Once the text is assigned to the variable, it can be manipulated or executed dynamically. This offers great flexibility for advanced programming tasks, such as generating dynamic reports, creating custom functions on the fly, or conditionally executing longer code blocks.
Data Analysis

Type : CREATION_INTERNE


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!
1PROC 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;
11RUN;
12QUIT;
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!
1PROC 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 PRINT DATA=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;
23RUN;
24QUIT;
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!
1PROC 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 */
17 SOURCE actionCaslDynamique;
18 BUILTINS.filter(TABLE={name='ventes', caslib='casuser'},
19 where='Region = &filtreRegion.');
20 ENDSOURCE;
21 
22 /* Affiche et exécute le code CASL généré */
23 PRINT 'Code CASL généré: ' actionCaslDynamique;
24 evaluate actionCaslDynamique;
25 
26 /* 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;
37RUN;
38QUIT;
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!
1PROC 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 */
13 %let creerTable = 1;
14 
15 SOURCE creationTableConditionnelle;
16 IF &creerTable. = 1 THEN DO;
17 BUILTINS.loadtable(path='donnees_source.sashdat', caslib='casuser', casout={name='donnees_chargees', replace=true});
18 PRINT 'Table donnees_chargees créée avec succès.';
19 END;
20 ELSE DO;
21 PRINT 'Création de table ignorée.';
22 END;
23 ENDSOURCE;
24 
25 evaluate creationTableConditionnelle;
26 
27 /* Scénario 2 : Exécution d'une action avec gestion d'erreur simulée */
28 SOURCE testActionAvecErreur;
29 try {
30 BUILTINS.droptable(name='table_non_existante', caslib='casuser', quiet=false);
31 PRINT 'Cette ligne ne devrait pas s''afficher.';
32 } catch (e) {
33 PRINT 'Erreur attrapée : ' || e.message;
34 }
35 ENDSOURCE;
36 
37 evaluate testActionAvecErreur;
38 
39 /* Nettoyage */
40 BUILTINS.droptable(name='donnees_source', caslib='casuser', quiet=true);
41 BUILTINS.droptable(name='donnees_chargees', caslib='casuser', quiet=true);
42RUN;
43QUIT;
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.