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
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.
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;
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.
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!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.