BookmarkSubscribeRSS Feed
Lupacante
Calcite | Level 5

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

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Kurt_Bremser
Super User

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;

data_null__
Jade | Level 19

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 804 views
  • 6 likes
  • 4 in conversation