BookmarkSubscribeRSS Feed
monicazhou2013
Calcite | Level 5

Array multi{1:2, 2}(1,2);

Do i=1 to 2;

Do j=1 to 2;

Output=multi{I,j};

What is the corresponding values of i, j, and output.

Someone gives the answer: A 2*2 multi-array, only two initial values, so :

 

 i   j  output

1  1  1

1  2  2

2  1   .

2  2   .

Could anybody explain how to get this result? 

2 REPLIES 2
Astounding
PROC Star

"Someone" gave you the correct answer.  But there's a lot to explain.  Why don't you begin by explaining the parts of the ARRAY statement that you already understand.  Then we can fill in the remainder without having to explain things that you already know.

mkeintz
PROC Star

Multi-dimensional arrays are stored in row-major order.  I.e all the elements of the first row precede the 2nd row, which precedes the 3rd row, etc.  And the first array index is the row index, the second is the column index. 

 

Your  statement

   array{1:2,2}

is the same as

   array{2,2};

which is 2 rows of 2-elements each.

 

The 2 initial values (1,2) are assigned to the first two elements of X, translating to elements  {1,1} and {1,2}.

 

If you run this program you will print out the memory addresses of each element, confirming the row-major order of array storage.

 

data _null_;
  array x{2,2} (1,2);
  add1_1=addrlong(x{1,1});
  add1_2=addrlong(x{1,2});
  add2_1=addrlong(x{2,1});
  add2_2=addrlong(x{2,2});
  put (add:) (=$hex16. /);
run;

Note that the addresses are "little-endian", i.e. the least significant byte (i.e. the least significant pair of hexadecimal digits) is the leftmost.  The addresses in my machine came out as:

9    data _null_;
10     array x{2,2} (1,2);
WARNING: Partial value initialization of the array x.
11     add1_1=addrlong(x{1,1});
12     add1_2=addrlong(x{1,2});
13     add2_1=addrlong(x{2,1});
14     add2_2=addrlong(x{2,2});
15     put (add:) (=$hex16. /);
16   run;

add1_1=9010682A00000000
add1_2=9810682A00000000
add2_1=A010682A00000000
add2_2=A810682A00000000
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 2 replies
  • 770 views
  • 0 likes
  • 3 in conversation