- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hello team,
I have been given a code to dissect it and it has an array statement: what is the reason we use an array statement?
array mbrs(*) & mbr.;
Thanks,
blue blue
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
The array statement creates the references that allow use of array related syntax. If you don't define an array then you can't use one.
Arrays allow several things. One is if you want to do the same calculation or block of statements with a group of values then the array allows. Another is to short hand a group of variables for use in such functions as sum, mean, call missing, whichn, whichc, and many others.
You can create look up values with arrays.
You can parallel process several related variables such as having an array visit dates and visit results.
You can write code to confuse new SAS users.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
There are several reasons but this is the most common: to perform the same statements over multiple variables. Arrays avoid having to repeat the statements for each variable. Consider this:
data want;
set have;
x1 = a * b;
x2 = a * b;
x3 = a * b;
x4 = a * b;
run;
And an array version:
data want;
set have;
array xarray (*) x1 - x4;
do i = 1 to dim(xarray);
xarray(i) = a * b;
end;
run;
Both programs do exactly the same processing. The array version avoids repeating statements.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@SASKiwi You meant to say "dim(xarray)".
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@WarrenKuhfeld wrote:
@SASKiwi You meant to say "dim(xarray)".
Probably the same mental block I have: Don't make array names longer than the variables by habit.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@WarrenKuhfeld Thanks - now corrected.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
The reason why an ARRAY is used is hard to discern without the context in which it is used.
In general the idea is repeat the same series of statements of each variable included in the array. I reduces the need to write wallpaper code.
data want;
set have ;
array measures m1-m100;
do over measures;
measures=measures**2 ;
end;
run;
Or you want to reference the variable from a list based on an index into that list that is not known it advance.
data want ;
set have;
array monthly jan feb mar apr may jun jul aug sep oct nov dec;
monthly[month(date)]=cost;
run;