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!
data _null_;
/* Déclaration d'un tableau dynamique EEL */
length myArray{3} $20;
myArray[1] = "Apple";
myArray[2] = "Banana";
myArray[3] = "Cherry";
/* Utilisation de la fonction GET pour récupérer un élément */
myElement = GET(myArray, 2);
put "L'élément à l'indice 2 est : " myElement; /* Attendu : Banana */
run;
1
DATA _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 */
11
RUN;
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!
data _null_;
/* Déclaration et initialisation d'un tableau dynamique EEL */
length salesData{*} 8;
salesData[1] = 100;
salesData[2] = 150;
salesData[3] = 200;
salesData[4] = 120;
/* Redimensionner le tableau pour ajouter un élément */
DIM(salesData) = 5;
salesData[5] = 180;
/* Itérer et afficher tous les éléments du tableau */
do i = 1 to DIM(salesData);
currentSale = GET(salesData, i);
put "Vente pour l'indice " i " : " currentSale;
end;
run;
1
DATA _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;
18
RUN;
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!
data _null_;
/* Déclaration d'un tableau dynamique EEL pour simuler une matrice 2x3 */
/* Les éléments sont stockés séquentiellement: (1,1) (1,2) (1,3) (2,1) (2,2) (2,3) */
length matrixData{6} 8;
matrixData[1] = 11; matrixData[2] = 12; matrixData[3] = 13;
matrixData[4] = 21; matrixData[5] = 22; matrixData[6] = 23;
numRows = 2;
numCols = 3;
/* Récupérer un élément spécifique (par exemple, élément à la ligne 2, colonne 1) */
targetRow = 2;
targetCol = 1;
calculatedIndex = ((targetRow - 1) * numCols) + targetCol;
if (calculatedIndex >= 1 and calculatedIndex <= DIM(matrixData)) then do;
element = GET(matrixData, calculatedIndex);
put "Élément à la ligne " targetRow ", colonne " targetCol " : " element; /* Attendu : 21 */
end;
else do;
put "Index hors limites pour la matrice.";
end;
/* Récupérer et afficher tous les éléments de la deuxième ligne */
put ' ';
put 'Éléments de la deuxième ligne :';
do j = 1 to numCols;
calculatedIndex_row2 = ((2 - 1) * numCols) + j;
element_row2 = GET(matrixData, calculatedIndex_row2);
put " Élément (2," j ") : " element_row2;
end;
run;
1
DATA _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) */
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!
/* Exemple 4 : Intégration Viya/CAS avec DS2 */
/* Création d'une table CAS en mémoire pour démonstration */
data casuser.input_data;
input id $ item1 $ item2 $ item3 $;
datalines;
101 Apple Banana Cherry
102 Orange Grape Kiwi
103 Mango Peach
;
run;
proc cas;
session casauto;
/* Charger la table dans CAS */
load data=casuser.input_data out=casuser.input_data;
/* Utiliser un bloc DS2 pour créer un tableau EEL et utiliser la fonction GET */
ds2;
data casuser.processed_items {overwrite=true};
method run();
set casuser.input_data;
declare character(20) fruitArray[3] _temporary_; /* Déclaration d'un tableau EEL temporaire */
declare character(20) secondFruit;
/* Peupler le tableau EEL avec les valeurs des colonnes */
fruitArray[1] = item1;
fruitArray[2] = item2;
fruitArray[3] = item3;
/* Utiliser la fonction GET pour récupérer le deuxième fruit */
if DIM(fruitArray) >= 2 then do;
secondFruit = GET(fruitArray, 2);
end;
else do;
secondFruit = 'N/A';
end;
output;
end;
enddata;
quit;
/* Afficher la table résultante */
print data=casuser.processed_items;
quit;
1
/* Exemple 4 : Intégration Viya/CAS avec DS2 */
2
/* Création d'une table CAS en mémoire pour démonstration */
/* 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) >= 2THENDO;
33
secondFruit = GET(fruitArray, 2);
34
END;
35
ELSEDO;
36
secondFruit = 'N/A';
37
END;
38
39
OUTPUT;
40
END;
41
enddata;
42
QUIT;
43
44
/* Afficher la table résultante */
45
PRINTDATA=casuser.processed_items;
46
QUIT;
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.
« 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. »
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.