Search notes:

SAS statements: array

Basics

An array statement does not create a new data structure. It creates a name by which other variables can be referenced.
The name of the underlying variable is returned by the vname function.
data _null_;

    array ary[3] foo bar baz;

    foo =  42;
    bar = 999;
    baz =   7;

    do i = 1 to dim(ary);
       ary[i] = ary[i]+i;
       put ary[i]=;

    /*  The loop prints:
          foo=43
          bar=1001
          baz=10

       NOTE although the put statement uses ary[i], the
       actual printed variable name are the underlying
       foo, bar and baz.

    */
    end;

    put "Changed value of foo = " foo;
    put "Changed value of bar = " bar;
    put "Changed value of baz = " baz;

/*
    The above puts print:
      Changed value of foo = 43
      Changed value of bar = 1001
      Changed value of baz = 10

    Again, the assignement to ary[i] in the do loop have
    changed the underlying variables.
*/
    
run;
Github repository about-SAS, path: /programming/statements/array/basics.sas

Initializing

data _null_;

  array ary[3] (42 999 17);

  do i=1 to dim(ary);
     put 'ary['i']: ' ary[i];
  end;
 
run;
Github repository about-SAS, path: /programming/statements/array/initialize.sas

Set length of array

data tq84_data;

  /* Set the length of each element of the array
     to 20 characters: */
  array num{*} $20 num_1-num_10;
  input num_1-num_10;

datalines;
one two three four five six seven eight nine ten
eins zwei drei vier fünf sechs sieben acht neun zehn
un deux trois quattre cinque six sept huit neuf dix
run;

proc print
     data=tq84_data;
run;
Github repository about-SAS, path: /programming/array/length.sas

Colon

data _null_;

   var_foo = 'Foo';
   var_bar = 'Bar';
   var_baz = 'Baz';

/* Variables with a common prefix (here: "var_")  can
   be referenced with a colon: */
   array ary{*} var_:;

   do i=1 to dim(ary);
      put 'ary ' i ': ' ary[i];
   end;
/* The put statement puts:
     ary 1 : Foo
     ary 2 : Bar
     ary 3 : Baz
*/

run;
Github repository about-SAS, path: /programming/statements/array/colon.sas

Of

data _null_;

  foo = 10;
  bar =  7;
  baz =  3;

  array ary{*} foo bar baz;

  sum_ = sum(of ary{*});

  put sum_=;

run;
Github repository about-SAS, path: /programming/statements/array/of.sas

Repetition factor

data _null_;

  /* Initalize array with 1,2,3,1,2,3 */
  array ary[6] (2 * 1:3);

  do i=1 to dim(ary);
     put 'ary[' i ']: ' ary[i];
  end;
 
run;
Github repository about-SAS, path: /programming/statements/array/repetition-factor.sas

Start index with 0

data _null_;

  foo =  42;
  bar = 999;
  baz =  17;

  /* Usually, the first index is 1. In the following
     statement, it's changed to 0 */
  array ary{0: 2} foo bar baz;

  do i = 0 to dim(ary)-1; /* Note the dim() - 1 */
     put i ': ' ary{i};
  end;

run;
Github repository about-SAS, path: /programming/statements/array/start-index-with-0.sas

See also

SAS programming: arrays
SAS statements
Using _numeric_ and _character_ with arrays.

Index