DATA Step, Macro, Functions and more

array length

Reply
N/A
Posts: 1

array length

Is it possible to create a array with length after calculations in data steps or in macro?
for example, i calculate the length of array required is array_length:
data;
a='asdfgh';
b='axmnbkl';
n=length(a);
m=length(b);
array_length=2*(n+m)+1;
array V(array_length);
run;

of course the above example will return errors, but it illustrate what i want to do.
Super Contributor
Super Contributor
Posts: 3,174

Re: array length

Posted in reply to justforgetit
The parameter you are attempting to set (subscript -- within the parentheses) has no bearing on the SAS variable value length at all. Re-visit the DOC.

Suggest you start over and explain what it is you want to accomplish with the SAS system and why.

Scott Barry
SBBWorks, Inc.

Suggested Google advanced search arguments, this topic / post:

array statement site:sas.com

array introduction site:sas.com
Valued Guide
Posts: 2,177

Re: array length

Posted in reply to justforgetit
hi justforgetit
the simple answer for you is NO

SAS data step arrays have a length that must be declared at compile-time and the kind of variables you demonstrate using, have no value at compile time.

Data step hash tables (also called associative arrays) define their memory at "run-time" and so these could be defined as you seem to seek. However hash tables are defined differently from your example.

good luck
peterC
SAS Super FREQ
Posts: 8,864

Re: array length

Posted in reply to justforgetit
Hi:

This paper is a good introduction to Array processing:
http://www2.sas.com/proceedings/forum2007/273-2007.pdf

Normally, you do not need to worry about the length of an array, because in SAS, an array is not a physical data construct (as it is in some languages). An array in SAS is a convenient way to reference a group of separate variables as though they were stored in an array.

The variables, when they are stored internally in a SAS dataset are stored by their individual names. For the duration of a DATA step program, however, you can treat a group of variables as though they were members of an array for ease of processing and reference. So these are all valid DATA step array references:
[pre]
array lovelucy $ fred ethel lucy ricky;
array regsl regsale1 regsale2 regsale3 regsale4;
[/pre]

In the first array, LOVELUCY, the character variables FRED, ETHEL, LUCY and RICKY are being treated as array members LOVELUCY(1), LOVELUCY(2), LOVELUCY(3) and LOVELUCY(4) respectively. While in the second ARRAY statement, the REGSL array will be composed of the individual variables REGSALE1, REGSALE2, REGSALE3 and REGSALE4 and can be treated as array members using the array syntax: REGSL(1), REGSL(2), REGSL(3) and REGSL(4).

I could also declare an array to be composed of all my character variables or all my numeric variables (which I might want to do if I was testing for the presence or absence of missing values):
[pre]
array cv $ _character_;
array nv _numeric_;
[/pre]

In a DATA step program you could use a DATA step DO loop to iterate through an array in order to perform some kind of processing. For the first 2 ARRAY statements, the number of variables in the array is known, but in the second set of ARRAY statements, the number of variables is unknown. However, the DIM function would allow me to operate a DO loop in the DATA step from 1 to the DIM(...) of the array.

Consider this program below, which treats all the numeric variables in SASHELP.CLASS like array members (program and log output shown here).

cynthia
[pre]
529
530 data testit;
531 set sashelp.class(obs=5);
532 array myv _numeric_;
533 do i = 1 to dim(myv);
534 thisvar = vname(myv(i));
535 put _n_= Name= i= thisvar= ' value is: ' myv(i);
536 end;
537 run;

_N_=1 Name=Alfred i=1 thisvar=Age value is: 14
_N_=1 Name=Alfred i=2 thisvar=Height value is: 69
_N_=1 Name=Alfred i=3 thisvar=Weight value is: 112.5
_N_=2 Name=Alice i=1 thisvar=Age value is: 13
_N_=2 Name=Alice i=2 thisvar=Height value is: 56.5
_N_=2 Name=Alice i=3 thisvar=Weight value is: 84
_N_=3 Name=Barbara i=1 thisvar=Age value is: 13
_N_=3 Name=Barbara i=2 thisvar=Height value is: 65.3
_N_=3 Name=Barbara i=3 thisvar=Weight value is: 98
_N_=4 Name=Carol i=1 thisvar=Age value is: 14
_N_=4 Name=Carol i=2 thisvar=Height value is: 62.8
_N_=4 Name=Carol i=3 thisvar=Weight value is: 102.5
_N_=5 Name=Henry i=1 thisvar=Age value is: 14
_N_=5 Name=Henry i=2 thisvar=Height value is: 63.5
_N_=5 Name=Henry i=3 thisvar=Weight value is: 102.5
[/pre]
Ask a Question
Discussion stats
  • 3 replies
  • 342 views
  • 0 likes
  • 4 in conversation