Desktop productivity for business analysts and programmers

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

Reply
Contributor
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?

Thanks,

Marco

Super User
Super User
Posts: 9,838

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

Posted in reply to Lupacante

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

Do:

ID      VARNUM     RESULT

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.

Super User
Posts: 10,568

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

Posted in reply to Lupacante

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
How to convert datasets to data steps
How to post code
Respected Advisor
Posts: 3,867

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

Posted in reply to Lupacante

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
  • 301 views
  • 6 likes
  • 4 in conversation