Matrices with Multiple Elements

To enter a matrix having multiple elements, use braces ({ }) to enclose the data values. If the matrix has multiple rows, use commas to separate them. Inside the braces, all elements must be either numeric or character. You cannot have a mixture of data types within a matrix. Each row must have the same number of elements.

For example, suppose you have one week of data on daily coffee consumption (cups per day) for four people in your office. Create a $4\times 5$ matrix called coffee with each person’s consumption represented by a row of the matrix and each day represented by a column. The following statements use the RESET PRINT command so that the result of each assignment statement is displayed automatically:

proc iml;
reset print;
coffee = {4 2 2 3 2,
          3 3 1 2 1,
          2 1 0 2 1,
          5 4 4 3 4};

Figure 5.2: A $4\times 5$ Matrix

coffee 4 rows 5 cols (numeric)

4 2 2 3 2
3 3 1 2 1
2 1 0 2 1
5 4 4 3 4


Next, you can create a character matrix called names with rows that contains the names of the coffee drinkers in your office. Notice in Figure 5.3 that if you do not use quotes, characters are converted to uppercase.

names = {Jenny, Linda, Jim, Samuel};

Figure 5.3: A Column Vector of Names

names 4 rows 1 col (character, size 6)

JENNY
LINDA
JIM
SAMUEL


Notice that RESET PRINT statement produces output that includes the name of the matrix, its dimensions, its type, and (when the type is character) the element size of the matrix. The element size represents the length of each string, and it is determined by the length of the longest string.

Next display the coffee matrix using the elements of names as row names by specifying the ROWNAME= option in the PRINT statement:

print coffee[rowname=names];

Figure 5.4: Rows of a Matrix Labeled by a Vector

coffee
JENNY 4 2 2 3 2
LINDA 3 3 1 2 1
JIM 2 1 0 2 1
SAMUEL 5 4 4 3 4