כמו במרבית שפות התכנות גם ב – SAS יש מערכים – Array – אבל ישנם מספר הבדלים בין מערך ב – SAS למערך ביתר שפות תכנות ולכן, לעיתים, מתכנתי SAS נתקלים בבעיות כאשר הם מנסים לעשות שימוש ביכולת הזאת ב – SAS.
מספר נקודות לזכור:
* ב – SAS מערך הוא פשוט אוסף של עמודות בטבלה ולא אובייקט בפני עצמו.
* בניגוד לשפות תכנות אחרות, מערכים ב – SAS מתחילים ב – 1 ולא ב – 0.
* כל העמודות במערך חייבות להיות מאותו סוג כלומר נומריות או טקסטואליות. לא ניתן לערבב במערך אחד עמודות מסוגים שונים.
נתחיל בכמה דוגמאות:
data CLASS1;
set SASHELP.CLASS;
array a1{2} weight height; * Explicity writing the number of columns in the array;
array a2{*} weight age; * Let SAS to find out by itself how many columns in the array;
array a3{*} name sex; * String columns - no problem;
array a4{*} age age; * We can use the same column twice;
if _n_=1 then do; * Printing value of the first row only just to illustrate;
put a1[1]=; * Print the first column in a1 array;
put a2[2]=; * Print the second column in a2 array;
put a3[*]; * Printing all the columns in array a3;
put a4[*]=; * Printing all the columns in array a4 with the column names;
end;
run;
ברגע שהגדרנו לנו מערך אנחנו יכולים לדעת כמה אובייקטים (עמודות) הוא מכיל ע"י הפונקציה DIM:
data CLASS2;
set SASHELP.CLASS;
array a{*} weight height;
x=dim(a);
run;
אנחנו יכולים להשתמש בלולאות של SAS כדי לעבור על המערך ולבצע פעולות על כל אחת מהעמודות המרכיבות אותו:
data CLASS3(drop=i);
set SASHELP.CLASS;
array a{*} weight height;
do i=1 to dim(a);
a[i]=a[i]*2;
end;
run;
אם נזכור את שלושת הנקודות לעיל נגלה שהמערכים ב – SAS הרבה יותר פשוטים ממה שנדמה ואיגוד עמודות למערך מאפשר לנו לפנות אליהן בקלות כך שהתוכניות שלנו יהיו פשוטות יותר ומובנות יותר.
חגי