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:
of course the above example will return errors, but it illustrate what i want to do.
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.
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:
array lovelucy $ fred ethel lucy ricky;
array regsl regsale1 regsale2 regsale3 regsale4;
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):
array cv $ _character_;
array nv _numeric_;
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).
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);
_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