DATA Step, Macro, Functions and more

Multidimensional Array with different columns length

Reply
Super Contributor
Posts: 318

Multidimensional Array with different columns length

Given that I have to create a multidimensional array of the below:

silo1 - storage 1, storage 2, storage 3

silo2 - storage 4, storage 5, storage 6, storage 7

If I create an array arrsilo [2,4] storage1- storage7 will get an error since it expects 8 variables. Is there a way I can create the array that will allow me to have the array have only 3 columns for row 1 then 4 columns for row 2?

Any other ways would greatly be appreciated. Thank you!

SAS Employee
Posts: 340

Re: Multidimensional Array with different columns length

No. 2 dimensional arrays are n1 x n2 tables.

But this is valid:

array arrsilo [2,4] storage1- storage3 dummy storage4 - storage7;

drop dummy;

Or you can use associative arrays: hash object.

Super Contributor
Posts: 318

Re: Multidimensional Array with different columns length

This is my current workaround although I was thinking of a more appropriate way.

How do you implement such has object? Greatly appreciate if you can give an example.

Super User
Posts: 9,682

Re: Multidimensional Array with different columns length

No way for SAS, But JAVA can do that .

Super Contributor
Posts: 318

Re: Multidimensional Array with different columns length

So true. Makes me miss Java because of this requirement.

Super User
Super User
Posts: 7,407

Re: Multidimensional Array with different columns length

Not sure what your problem is or what you are trying to achieve as you already have the array:

I assume you have a table like:

STORAGE1 STORAGE2...STORAGE7.  Now that in itself is an array{7} of variables named storage.  If you then want these in two rows your end product will look like:

ID       VAR1        VAR2        VAR3       VAR4

silo1 - storage 1, storage 2, storage 3

silo2 - storage 4, storage 5, storage 6, storage 7

All an array is in terms of SAS is a convinient reference to variables in your dataset, so in the above VAR{x} is the array and refers to VAR1, VAR2 etc.  You could have an array refence to COL1, ABC1, EDC1 etc,

Super Contributor
Posts: 318

Re: Multidimensional Array with different columns length

If I'll be using a normal array I won't be able to identify how many storages I have for each silo based on my example fields. Because I would be creating a different logic/treatment depending on the number of silos.

Super User
Super User
Posts: 7,407

Re: Multidimensional Array with different columns length

Simple:

ID       VAR1        VAR2        VAR3       VAR4          No_Storage

silo1   storage 1,  storage 2, storage 3                     3

silo2   storage 4,  storage 5, storage 6, storage 7    4

I agree with Reeza, clarify what you are trying to do.  Different technologies approach problems in differing ways.

Super User
Posts: 17,865

Re: Multidimensional Array with different columns length

Perhaps restate your actual issue then and a more appropriate SAS solution can be offered.

Because SAS processes data line by line rather than loading the entire dataset into memory means it operates differently, sometimes that's good and sometimes that's bad.  But it does mean the best way to handle a certain scenario may differ from a memory based language.

Ask a Question
Discussion stats
  • 8 replies
  • 328 views
  • 0 likes
  • 5 in conversation