notes.016a  October 18, 2002

Some Basic Facts About IML.

==============================================================================

1.  How to define an array of a specified size:

    Example 1:  x = {1.1  1.0  2.3  3.2  4.5}  defines a 1 x 5 array.

    Example 2:  x = {1.1, 1.0, 2.3, 3.2, 4.5} defines a 5 x 1 array.

    Example 3:  x = {1 2 3, 4 5 6} defines a 2 x 3 array:

                    | 1   2   3 |
                x = |           |
                    | 4   5   6 |


    Example 4:  x = j(2, 3, 0) creates a 2 x 3 matrix of all 0's.

    Example 5:  x = j(3) creates a 3 x 3 matrix of all 1's.

    Example 6:  x = j(3, 3, 0) creates a 3 x 3 matrix of all 0's.

    Example 7:  x = 7:10 ;  creates a 1 x 4 matrix x = {7  8  9  10}.


2.  Elementwise specifications:

    Example 8:  x = j(3, 3, 0);
                x[1, 1] = 3 ; x[1, 2] = 5 ; x[1, 3] = 7 ;
                x[2, 1] = 11; x[2, 2] = 13; x[2, 3] = 17 ;
                x[3, 1] = 19; x[3, 2] = 23; x[3, 3] = 29 ;

       These statements create the matrix:

                    |  3    5    7 |
                    |              |
                x = | 11   13   17 |.
                    |              |
                    | 19   23   29 |


     Example 9: y = x[2, 3], with x as above.  Note y = 17.

3.  Operations:

    3.1  Addition of matrices:

         x = {1 2, 3 4} + {3 2, 2 1} = {5 5, 5 5}.

         Note: dimensions must match.

         Subtraction is like addition ...

    3.2  Multiplication of matrices:

         x = {1 2, 3 4} * {3 2, 2 1} = {7 4, 17 10}.

    3.3  Elementwise multiplication of matrices:

         x = {1 2, 3 4} # {3 2, 2 1} = {3 4, 6 4}.

    3.4  Elementwise power of matrices:

         x = {1 2, 3 4}##2 = {1 4, 9 16}.

    3.5  Maximum of two matrices:

         x = {1 2, 3 4} <> {3 2, 2 1} = {3 2, 3 4}.

         Minimum " >< " is similar.

    3.6  Transpose of a matrix:

         If x = {1 2, 3 4}, x` = {1 3, 2 4}.

         Note: this is the backwards-single-quote mark, not
         same as: '.

    3.7  Inverse of a square matrix:

         x = inv{1 2, 3 4} = {-2  1, 1.5  -.5}.

    3.8  Determinant of a square matrix:

         x = det{1 2, 3 4} = -2.

    3.9  Exponential of a matrix:

         x = exp{1 2, 3 4} = {exp(1)  exp(2), exp(3)  exp(4)}.

    3.10 Trace of a square matrix (sum of diagonal terms):

         If x = {1  2, 3  4}, then trace(x) = 1 + 4 = 5.

    3.11 Sum of elements of a matrix:  sumx = sum(x) :

         If x = {1 2 3 4}, then sum(x) = 1 + 2 + 3 + 4 = 10.

4.  Mistakes ...

    x = {1 2, 2, 1}.

    x = {1 2} * {1 2}  (should be: x = {1 2} * {1 2}`).

5.  Subsets of matrices:

            | 3  5   7 |
    If  y = |          |
            | 7  6  13 |,


    then x = y[2, ] = 2nd row = {7  6  13}.

         z = y[ ,2] = 2nd column = {5, 6}.

6.  Joining matrices together vertically:

    If y = {1 2, 3 4} and x = {11 19}, then

                | 1  2 |
                |      |
       y // x = | 3  4 | = [1 2, 3 4, 11 19}.
                |      |
                |11 19 |

    Note: dimensions must match properly for this to work.

7.  Joining matrices together horizontally:

    If x = {1  2, 3  4}  and y = {11, 17},

    then x || y = {1  2  11, 3  4  17}.

8.  More examples -

    if x = {1 2 3, 4 5 6, 7 8 9}  and

       y = x[1, ]          and

       z = x[, 1]          and

       v = x[, 2] || x[, 3]  then


           | 1  2  3 |
       x = | 4  5  6 |  ,
           | 7  8  9 |


       y = |1 2 3|,


           | 1 |
       z = | 4 | ,
           | 7 |

           | 2 |
       w = | 5 | ,
           | 8 |

           | 2  3 |
       v = | 5  6 |
           | 8  9 | .


9.  Functions of matrices, etc.

    8.1 Number of rows of a matrix   :  r = nrow(x) ;
    8.2 Number of columns of a matrix:  c = ncol(x) ;
    8.3 Sum of elements of a matrix: s = sum(x) ;
    8.4 Sum of squares of elements of a matrix: s = ssq(x) ;
    8.5 Absolute value :  absx = abs(x) ;


10. Miscellaneous.

   10.1  Suppose you want to create a 5 x 5 matrix that has the
         the numbers 1, 2, 3, 4, 5 down the main diagonal and 0's
         elsewhere:

         x = j(5, 5, 0) ;

         do i = 1 to 5 ;
            x[i, i] = i ;
         end ;

         print x ;

         Another way to do this:

          b = {1 2 3 4 5} ;
          diagb = diag(b) ;

   10.2  Suppose x and y are n x 1 column vectors, and you want to
         set the i-th element of x equal to a random element of y:

         r = nrow(y) ;

         i = 5 ;

         rand = 1 + int(r * ranuni(-1)) ;

         (note rand is a random integer between 1 and r)

         x[i] = y[rand] ;



11.  Eigenvalues and eigenvectors:

     For a square n x n  matrix A,

     call eigen(x, y, A) ;

     will create an n x 1 vector x of eigenvalues,

             and an n x n matrix y of eigenvectors.


     x = eigval(A) will compute eigenvalues of a square
         matrix A.

     x = eigvec(A) will compute an n x n matrix whose
         columns are eigenvectors of A.


12.  Getting data into iml:


     Create a dataset in 'ordinary' SAS, then 'use' the
     dataset in proc iml.  Read the variables from the dataset into
     vectors or arrays in proc iml.

     Example:

==============================================================================
 DATA lhs ;
      infile '/home/gnome/john-c/5421/lhs.data' ;


      INPUT CASENUM  AGE GENDER BASECIGS GROUP RANDDATE DEADDATE DEADCODE
            BODYMASS F31MSTAT
            VPCQUIT1 VPCQUIT2 VPCQUIT3  VPCQUIT4 VPCQUIT5
            CIGSA0   CIGSA1   CIGSA2    CIGSA3   CIGSA4   CIGSA5
            S1MFEV   S2FEVPRE  A1FEVPRE  A2FEVPRE A3FEVPRE A4FEVPRE A5FEVPRE
                     S2FEVPOS  A1FEVPOS  A2FEVPOS A3FEVPOS A4FEVPOS A5FEVPOS
                     WEIGHT0   WEIGHT1   WEIGHT2  WEIGHT3  WEIGHT4  WEIGHT5 ;

 RUN ;

 proc iml ;

      use lhs ;

      read all var {age gender} into x ;
      read all var {s2fevpos}   into y ;

      etc.

 quit ;

=============================================================================

       The iml section above "uses" the dataset lhs, and puts the
two variables, age and gender, into a 500 x 2 array.  It reads the
one variable  s2fevpos  from lhs and puts the result into a 500 x 1 
column vector.

========================================================================

12.  Writing a dataset out of iml:

     Writing data out of proc iml is accomplished by the use of the
     "create" statement.  For example, in proc iml, the following code
     creates a dataset called 'yobs':


     Example:  within proc iml:

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

     varnames = {'y1'  'y2'} ;                 * Create  a 1 x 2 array of labels ...
     create yobs from V [colname = varnames];  * V is an  n x 2 array ...
     append from V ;                           * read data from V into yobs

     quit ;

     data yobs; set yobs;                      * dataset yobs has variables y1, y2 on it.
          ysum = y1 + y2 ;

          etc.

     run ;


===============================================================================

/home/gnome/john-c/5421/notes.016a     Last date revised: October 12, 2011.