SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
GN0001
Barite | Level 11

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

Blue Blue
6 REPLIES 6
ballardw
Super User

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.

 

SASKiwi
PROC Star

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.

WarrenKuhfeld
Ammonite | Level 13

@SASKiwi You meant to say "dim(xarray)".

ballardw
Super User

@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.

Tom
Super User Tom
Super User

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;

 

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1379 views
  • 4 likes
  • 5 in conversation