Published on :
Macro CREATION_INTERNE

GET Function (Expression Engine Language)

This code is also available in: Deutsch Español Français
Awaiting validation
The GET function is an essential Expression Engine Language (EEL) function, allowing access to individual elements within a dynamic array. Arrays in EEL are 1-indexed. To use this function, you must specify the name of the dynamic array and the numerical index of the element to retrieve. It is fundamental for fine-grained manipulation of structured data in arrays within EEL contexts, such as DATA steps, DS2 procedures, or other SAS© Viya data processing environments. Judicious use of GET, often combined with the DIM function for managing array bounds, ensures safe and efficient data access.
Data Analysis

Type : CREATION_INTERNE


The provided examples use internally declared and initialized EEL arrays, or data generated via `datalines` in DATA or DS2 steps. This ensures the autonomy and executability of the examples without dependence on external data sources.

1 Code Block
DATA STEP Data
Explanation :
This simple example declares an EEL dynamic array 'myArray' of string type with three elements. The GET function is then used with index 2 to retrieve the second element ('Banana'), which is then displayed in the SAS log.
Copied!
1DATA _null_;
2 /* Déclaration d'un tableau dynamique EEL */
3 LENGTH myArray{3} $20;
4 myArray[1] = "Apple";
5 myArray[2] = "Banana";
6 myArray[3] = "Cherry";
7 
8 /* Utilisation de la fonction GET pour récupérer un élément */
9 myElement = GET(myArray, 2);
10 put "L'élément à l'indice 2 est : " myElement; /* Attendu : Banana */
11RUN;
2 Code Block
DATA STEP Data
Explanation :
This example illustrates a common use of the GET function within a loop. It shows how to iterate over a dynamic array using the DIM function to determine its size, and how GET allows access to each element sequentially. Array resizing is included to demonstrate its flexibility.
Copied!
1DATA _null_;
2 /* Déclaration et initialisation d'un tableau dynamique EEL */
3 LENGTH salesData{*} 8;
4 salesData[1] = 100;
5 salesData[2] = 150;
6 salesData[3] = 200;
7 salesData[4] = 120;
8
9 /* Redimensionner le tableau pour ajouter un élément */
10 DIM(salesData) = 5;
11 salesData[5] = 180;
12 
13 /* Itérer et afficher tous les éléments du tableau */
14 DO i = 1 to DIM(salesData);
15 currentSale = GET(salesData, i);
16 put "Vente pour l'indice " i " : " currentSale;
17 END;
18RUN;
3 Code Block
DATA STEP Data
Explanation :
This advanced example illustrates how the GET function can be used to simulate access to elements in a two-dimensional array stored as a one-dimensional array. An index calculation is performed to map 2D coordinates to a 1D index, and boundary checks are included for increased robustness. The second part of the example shows how to retrieve and display an entire row of this simulated array.
Copied!
1DATA _null_;
2 /* Déclaration d'un tableau dynamique EEL pour simuler une matrice 2x3 */
3 /* Les éléments sont stockés séquentiellement: (1,1) (1,2) (1,3) (2,1) (2,2) (2,3) */
4 LENGTH matrixData{6} 8;
5 matrixData[1] = 11; matrixData[2] = 12; matrixData[3] = 13;
6 matrixData[4] = 21; matrixData[5] = 22; matrixData[6] = 23;
7 
8 numRows = 2;
9 numCols = 3;
10 
11 /* Récupérer un élément spécifique (par exemple, élément à la ligne 2, colonne 1) */
12 targetRow = 2;
13 targetCol = 1;
14 calculatedIndex = ((targetRow - 1) * numCols) + targetCol;
15
16 IF (calculatedIndex >= 1 and calculatedIndex <= DIM(matrixData)) THEN DO;
17 element = GET(matrixData, calculatedIndex);
18 put "Élément à la ligne " targetRow ", colonne " targetCol " : " element; /* Attendu : 21 */
19 END;
20 ELSE DO;
21 put "Index hors limites pour la matrice.";
22 END;
23 
24 /* Récupérer et afficher tous les éléments de la deuxième ligne */
25 put ' ';
26 put 'Éléments de la deuxième ligne :';
27 DO j = 1 to numCols;
28 calculatedIndex_row2 = ((2 - 1) * numCols) + j;
29 element_row2 = GET(matrixData, calculatedIndex_row2);
30 put " Élément (2," j ") : " element_row2;
31 END;
32RUN;
4 Code Block
PROC CAS / DS2 Data
Explanation :
This example illustrates the use of the GET function in a SAS Viya environment with CAS (Cloud Analytic Services). A DS2 block is used to process a CAS table loaded into memory. Inside the DS2 program, a dynamic EEL array is declared and populated with data from the columns of the input table. The GET function is then applied to extract a specific element from the EEL array, and the result is saved to a new CAS table. The 'cas' flag is set to 1 to indicate the use of CAS functionalities.
Copied!
1/* Exemple 4 : Intégration Viya/CAS avec DS2 */
2/* Création d'une table CAS en mémoire pour démonstration */
3DATA casuser.input_data;
4 INPUT id $ item1 $ item2 $ item3 $;
5 DATALINES;
6 101 Apple Banana Cherry
7 102 Orange Grape Kiwi
8 103 Mango Peach
9 ;
10RUN;
11 
12PROC CAS;
13 SESSION casauto;
14 
15 /* Charger la table dans CAS */
16 load DATA=casuser.input_data out=casuser.input_data;
17 
18 /* Utiliser un bloc DS2 pour créer un tableau EEL et utiliser la fonction GET */
19 ds2;
20 DATA casuser.processed_items {overwrite=true};
21 method RUN();
22 SET casuser.input_data;
23 declare character(20) fruitArray[3] _temporary_; /* Déclaration d'un tableau EEL temporaire */
24 declare character(20) secondFruit;
25 
26 /* Peupler le tableau EEL avec les valeurs des colonnes */
27 fruitArray[1] = item1;
28 fruitArray[2] = item2;
29 fruitArray[3] = item3;
30 
31 /* Utiliser la fonction GET pour récupérer le deuxième fruit */
32 IF DIM(fruitArray) >= 2 THEN DO;
33 secondFruit = GET(fruitArray, 2);
34 END;
35 ELSE DO;
36 secondFruit = 'N/A';
37 END;
38 
39 OUTPUT;
40 END;
41 enddata;
42 QUIT;
43 
44 /* Afficher la table résultante */
45 PRINT DATA=casuser.processed_items;
46QUIT;
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


Banner
Expert Advice
Expert
Michael
Responsable de l'infrastructure Viya.
« Always wrap your GET calls in a boundary check (e.g., if index <= DIM(array)). In high-concurrency environments like CAS, an "Array Index Out of Bounds" error can be difficult to debug. Defensive programming ensures your data pipelines remain resilient and predictable. »