Desktop productivity for business analysts and programmers

Making an array of a yet-to-be-defined size

Posts: 27

Making an array of a yet-to-be-defined size

Hi all,

say I want to make an array but don't know how it will be yet (this will depend on later-defined parameters). How can I do this?



Esteemed Advisor
Esteemed Advisor
Posts: 7,249

Re: Making an array of a yet-to-be-defined size

You could use the function redim to re-size the array at runtime.  Or alternatively, write your code in such a way as to know the number of elements up front which would be my suggestion.  If you want to have data with an unknown amount of elements, then look at normalizing your data:

So rather than:

ID     VAR1     VAR2     VAR3     VAR4...

1          1               2          3          4



1               1               1

1               2               2    

1               3               3

1               4               4


That way you can have any number of elements and process them rapidly without needing to know how many there are.

Esteemed Advisor
Posts: 6,699

Re: Making an array of a yet-to-be-defined size

If you have variables named like name1 name2 ... namex then you can define an array as array vars{*} name1-namex;

If you want an array for all numeric variables, you can do array vars {*} _numeric_;

If it is more complex than that, you will have to determine the list of variables beforehand and put that into a macro var, so you can then do array vars{*} &list;

Maxims of Maximally Efficient SAS Programmers
Respected Advisor
Posts: 3,775

Re: Making an array of a yet-to-be-defined size

In addition to the good advice you have received on normalization and using "SAS Variables Lists" in array statements.  You can also consider using an associative array (HASH).  This is only only way to have a truly dynamic object that you can reference like an array.

SAS arrays are really just a special type of variable list and all variables must be known when a data step is compiled.

I too seek the holy grail of dynamic variable creation provide by the magic of PROC TRANSPOSE.

Ask a Question
Discussion stats
  • 3 replies
  • 4 in conversation