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 "the 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.

 

2 REPLIES 2
Patrick
Opal | Level 21

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

 

Not sure where you found the definition that you can also use a variable. In the SAS documentation I've looked it up the definition is as follows:

{subscript}

describes the number and arrangement of elements in the array by using an asterisk, a number, or a range of numbers.

https://go.documentation.sas.com/?docsetId=lestmtsref&docsetTarget=p08do6szetrxe2n136ush727sbuo.htm&... 

 

Syntax as you've posted it using a SAS data step variable as array subscript can't work because the array statement gets compiled and at this time there is no data from an input data set available. The only variable you could use is a SAS Macro variable.

https://go.documentation.sas.com/?docsetId=lrcon&docsetTarget=p08a4x7h9mkwqvn16jg3xqwfxful.htm&docse... 

 

Also: Don't use the double dash notation to reference variables a--c like you've done it unless you know exactly what you're doing.

array test{c} a-- c;

The double dash notation references variable in the sequence as they are in the program data vector (PDV). So if the variable in your PDV are for example in the sequence of B X A Z Y C D then with a double dash you'd include variables A Z Y C in the array and not A B C.

 

 

Patrick
Opal | Level 21

@webart999ARM 

I just realized that you've posted your question twice and got already answers here: https://communities.sas.com/t5/SAS-Programming/How-to-deal-with-Array-subscript-issue/m-p/581113#M16...

 

Please post questions only once.

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
  • 2 replies
  • 523 views
  • 0 likes
  • 2 in conversation