BookmarkSubscribeRSS Feed
webart999ARM
Quartz | Level 8

Hi Everybody.

I have some confusing situation regarding SAS Arrays.
The question is following.

Syntax ->  array array-name {n} <$> array-elements <(initial-values)>;

In the multiple SAS guides and topics about arrays, the definition of {n} is following `
" N is the array subscript in the array definition and it refers to the number of elements within the array. A numeric
constant, a variable whose value is a number, a numeric SAS expression, or an asterisk (*) may be used as the
subscript. "

Yes, the confusing is the underlined part.
I have tried that part in practice.

data test;
	input a b c;
	c=3;
	    cards;
		1 2 3
		4 5 6
		7 8 9
	    ;
run;

data test1;
     set test;
         array test{c} a-- c; /*Assuming the part where says "a variable whose value is a number*/
run;

 

After running following program, the log look like this`

---------------------------------------LOG------------------------------------------------
 array test{c} a-- c;
            -
            22
            202
ERROR: Too many variables defined for the dimension(s) specified for the array test.
ERROR 22-322: Syntax error, expecting one of the following: an integer constant, *.
ERROR 202-322: The option or parameter is not recognized and will be ignored.

------------------------------------------------------------------------------------------

And for second part, which assume "a numeric SAS expression", I have no ideas what it could be, 1+1+1 a new variable which created with a numeric expression?
Could you please help me on this, maybe I'm doing something wrong?
Thank you in advance.
 

4 REPLIES 4
ballardw
Super User

You can reference an array element with a variable. You can not define an array with a variable. You either explicitly state a size or if using a list of variables do not define a size but let the number of elements in the list define the size.

 

 

 

Post log and code entries into code windows opened on the forum with the {I} or "running man" icons to preserve formatting.

The error is fairly clear:

365  data work.test1;
366       set work.test;
367           array test{c} a-- c;
                         -
                         22
                         202
ERROR: Too many variables defined for the dimension(s) specified for the array test.
ERROR 22-322: Syntax error, expecting one of the following: an integer constant, *.

ERROR 202-322: The option or parameter is not recognized and will be ignored.

368  run;

The underlined C and error 322 is telling that you need either an actual integer, i.e.3 or *

the Code should be

data work.test1;
     set work.test;
     array test{*} a-- c;
run;

Using a VARIABLE integer to reference elements of an array:

data work.test1;
     set work.test;
     array test{*} a-- c;
     do i = 1 to dim(test);
      x= test[i];
      put _n_= I= x=;
    end;
run;

The variable I takes on values of 1 , 2 and 3. _n_ is an automatic variable that indicates, in this case, which record has been read from the input set.

KachiM
Rhodochrosite | Level 12

There are two types of Arrays in SAS. One is Named Array and the other is Nameless Array. The former deals with SAS variable-names as a shortcut to represent individual variables. The latter is called _Temporary_ Array. The _temporary_ arrays require the dimension (size) of the array at Compilation phase of a data step.

 

Your example mixed both the usages. For instance,

 


data test1;
     set test;
         array test{c} a-- c; /*Assuming the part where says "the variable whose value is a number*/
run;

You are saying that array TEST has the dimension of C where C comes as part of the data set TEST. The value of C (your 3) is not available at the compilation phase of the Data Step (Data Test1). It needs a constant value as 3. See the following:

 


data test1;
     set test;
         array test{3} a-- c; /*Assuming the part where says "the variable whose value is a number*/
run;

This works happily - no errors. 

 

In case of Named array, a better way to declare an array is:

 


data aa;
   set test;
   array test[*] a -- c;
run;

Here the STAR takes care of the size of the array. Even a better way will be:

 


data aa;
   set test;
   array test a -- c;
run;

The size of the array is implicitly computed at the time of compilation phase.

PGStats
Opal | Level 21

You are confusing array declaration and array reference.

 

Array declaration is where you define the array, it is processed during datastep compilation, before any execution. At that point SAS needs to know how many elements your array will have. For example,

 

array a{10};

array a a1-a10;

 

are valid and equivalent array declarations.

 

Array reference occurs during datastep execution. At that point you tell SAS which element of the array is referred by your expression with an index value. The index value must resolve to a number. For example, following the declaration above,

 

i = 10;

a{i} = a1 + a{5+5};

 

adds the value of the first array element to the tenth array element.

PG
webart999ARM
Quartz | Level 8

Thank you all for your answers.

As in the last answer said, "I confused array declaration and array reference".

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 2288 views
  • 3 likes
  • 4 in conversation