Lesson 12, Part 1: Matrix Operations and Functions in SAS/IML¶

  • SAS/IML Basics: Comparison with DATA Steps
  • Statements, Operations, and Functions in SAS/IML
  • Ways to Create Matrices
  • Subscript Operations
  • Ways to Extract Elements from Matrices
  • Creating SAS Data Sets from Vectors and Matrices
  • Data Processing in SAS/IML
  • Accessing Rows and Columns by Names

Print the top rows of your SAS data by Rick Wicklin

Example 1 (Creating vectors)¶
In [6]:
proc IML;
Col_vector1 = {2, 4, 6, 8};   /** 4 X 1 matrix, column vector,
                                        evenly spaced values **/
Col_vector2 = j(4, 1, 5);     /** 4 X 1 matrix, 
                                  column vector of 5's  **/
Row_vector1 = 1:3;        /** 1 X c matrix, row vector, 
                                  increasing seq. of numbers **/
Row_vector2 = 1:-1;           /** 1 X c matrix, row vector, 
                                  decreasing seq. of numbers **/
print Col_vector1 Col_vector2 Row_vector1 Row_vector2;
quit;
SAS Output

The SAS System

Col_vector1 Col_vector2 Row_vector1   Row_vector2  
2 5 1 2 3 1 0 -1
4 5            
6 5            
8 5            

Example 2 (Creating vectors using a Do function)¶

In [6]:
* Ex1_creating_Vectors.sas (Part 2);
proc IML;
Row_vector3 = do(10, 70, 20); /** 1 X c matrix, row vector,
                                  positive increment **/
Row_vector4 = do(15, -10, -5);/** 1 X c matrix, row vector,
                                  negative increment **/
x_scalar = 5;                 /** 1 X 1 matrix, scalar **/

mat_A = {1 2 3, 4 5 6};           

print Row_vector3 Row_vector4 x_scalar  mat_A;
quit;
Out[6]:
SAS Output

The SAS System

Row_vector3   Row_vector4   x_scalar
10 30 50 70 15 10 5 0 -5 -10 5
mat_A
1 2 3
4 5 6

Example 3 (Creating row and column vectors for a matrix)¶

In [7]:
* Ex1_creating_Vectors.sas (Part 3);
proc iml;
      s= 2; /*scalar matrix - just one element*/
     rv = {1 2 3};  /* 1 x 3 row vector */
     cv = {1,2,3};  /* 3 X 1 column vector */
     mat = {1 2 3,4 5 6, 7 8 9} ;  /*3 X 3 matrix */
    print  s rv cv mat;
 quit;
Out[7]:
SAS Output

The SAS System

s rv   cv mat  
2 1 2 3 1 1 2 3
        2 4 5 6
        3 7 8 9

Example 4 (Matrix addition)¶

In [3]:
*Ex2_matrix_addition.sas;
*ods exclude all;
ods graphics off; ;
options nodate nonumber;
proc iml;
    M1 = {1 2 3,4 5 6, 7 8 9};  *3 X 3 matrix ;
    M2 = {7 8 9, 4 5 6, 1 2 3};  *3 X 3 matrix;
    M1_M2_Addition = M1+M2; *Matrix addition;
print M1 M2 M1_M2_Addition;
quit;
Out[3]:
SAS Output

The SAS System

M1   M2   M1_M2_Addition  
1 2 3 7 8 9 8 10 12
4 5 6 4 5 6 8 10 12
7 8 9 1 2 3 8 10 12

Example 5 (Matrix subtraction)¶

In [4]:
ods graphics off; 
options nodate nonumber;
proc iml;
    M1 = {1 2 3,4 5 6, 7 8 9} ;  *3 X 3 matrix; 
    M2 = {7 8 9, 4 5 6, 1 2 3} ;  *3 X 3 matrix ;
    M1_M2_Subtract = M1-M2; *Matrix Subtraction; 
    print M1 M2 M1_M2_Subtract;
quit;
Out[4]:
SAS Output

The SAS System

M1   M2   M1_M2_Subtract  
1 2 3 7 8 9 -6 -6 -6
4 5 6 4 5 6 0 0 0
7 8 9 1 2 3 6 6 6

Example 6 (Matrix multiplication)¶

In [7]:
options nocenter nodate nonumber;
proc iml;
    M1 = {1 2 3,4 5 6, 7 8 9} ;  *3 X 3 matrix; 
    M2 = {7 8 9, 4 5 6, 1 2 3} ;  *3 X 3 matrix ;
    M1_M2_E_Times= M1#M2; *Elemetwise multplication;
   print M1 M2 M1_M2_E_Times;
quit;
Out[7]:
SAS Output

The SAS System

M1   M2   M1_M2_E_Times  
1 2 3 7 8 9 7 16 27
4 5 6 4 5 6 16 25 36
7 8 9 1 2 3 7 16 27

Example 7 (Raising power to a matrix)¶

In [1]:
options nocenter nodate nonumber;
proc iml;
    M1 = {1 2 3,4 5 6, 7 8 9} ;  *3 X 3 matrix; 
    M1_E_Power2= M1##2; 
print M1 M1_E_Power2;
quit;
SAS Connection established. Subprocess id is 2640

Out[1]:
SAS Output

The SAS System

M1   M1_E_Power2  
1 2 3 1 4 9
4 5 6 16 25 36
7 8 9 49 64 81

Example 8 (Elementwise exponentiation in IML)¶

In [15]:
options nodate nonumber;

proc iml;
    M1 = {1 2 3 4 5,
          6 7 8 9 10};

    M1_E_sq_root = M1##0.5;  /* Elementwise square root */

    print M1 M1_E_sq_root;
quit;
SAS Output

The SAS System

M1   M1_E_sq_root  
1 2 3 4 5 1 1.4142136 1.7320508 2 2.236068
6 7 8 9 10 2.4494897 2.6457513 2.8284271 3 3.1622777

Example 9 (Scalar addition in IML)¶

In [3]:
options nocenter nodate nonumber;
options nocenter nodate nonumber;

proc iml;
    M1 = {1 2 3,
          4 5 6,
          7 8 9};

    M1_Scalar_Addition = M1 + 2;  /* Scalar addition */

    print M1 M1_Scalar_Addition;
quit;
Out[3]:
SAS Output

The SAS System

M1   M1_Scalar_Addition  
1 2 3 3 4 5
4 5 6 6 7 8
7 8 9 9 10 11

Example 10 (Scalar subtraction in IML)¶

In [4]:
options nocenter nodate nonumber;
proc iml;
      M1 = {1 2 3,4 5 6, 7 8 9} ;  
      M1_Scalar_Subtract= M1-2; *Scalar subtraction;
 print M1 M1_Scalar_Subtract;
quit;
Out[4]:
SAS Output

The SAS System

M1   M1_Scalar_Subtract  
1 2 3 -1 0 1
4 5 6 2 3 4
7 8 9 5 6 7

Example 11 (Scalar division in IML)¶

In [5]:
options nocenter nodate nonumber;
proc iml;
      M1 = {1 2 3,4 5 6, 7 8 9} ;  
      M1_Scalar_Division= M1/2; *Scalar division;
  print M1 M1_Scalar_Division;
quit;
Out[5]:
SAS Output

The SAS System

M1   M1_Scalar_Division  
1 2 3 0.5 1 1.5
4 5 6 2 2.5 3
7 8 9 3.5 4 4.5

Example 12 (Keep only rows with no missing numeric values)¶

Complete cases: How to perform listwise deletion by Rick Wicklin

In [23]:
ods html close;
options nocenter nodate nonumber nosource;  

data heart;
    set sashelp.heart;
    if not nmiss(of _numeric_);
run; 

proc print data=heart (obs=5);
run;
SAS Output

The SAS System

Obs Status DeathCause AgeCHDdiag Sex AgeAtStart Height Weight Diastolic Systolic MRW Smoking AgeAtDeath Cholesterol Chol_Status BP_Status Weight_Status Smoking_Status
1 Dead Cancer 56 Male 56 67.25 122 72 120 87 15 72 194 Desirable Normal Underweight Moderate (6-15)
2 Dead Coronary Heart Disease 74 Male 46 66.50 157 84 142 116 30 76 233 Borderline High Overweight Very Heavy (> 25)
3 Dead Coronary Heart Disease 71 Female 49 60.50 153 110 196 140 5 73 221 Borderline High Overweight Light (1-5)
4 Dead Coronary Heart Disease 67 Female 49 61.00 142 92 138 127 30 75 276 High High Overweight Very Heavy (> 25)
5 Dead Coronary Heart Disease 73 Male 55 67.50 193 60 148 138 15 75 242 High High Overweight Moderate (6-15)

Example 13 (Computing SSCP matrix)¶

In [27]:
options nocenter nodate nonumber;  

/* Ensure dataset exists */
data cars;
    set sashelp.heart;
    if nmiss(of _numeric_) = 0;
run;

proc iml;
    use heart;  
    read all var _NUM_ into X[c=varNames];  
    close;

    n = nrow(X);
    p = ncol(X);

    SSCP = X` * X;   /* Sum of Squares and Cross Products */

    print n p, SSCP;
quit;
SAS Output

The SAS System

n p
864 10
SSCP
3578594 2751618 3586542.5 8955902 5024886 8266664 6896582 592885 4010500 13442229
2751618 2161618 2786038 6970246 3919260 6461146 5369522 460691 3110916 10472891
3586542.5 2786038 3706343.4 9289157.3 5172067 8469097.8 7077253.8 660062.25 4054182.5 13845287
8955902 6970246 9289157.3 23872962 13051009 21361926 18147704 1658090 10135618 34679957
5024886 3919260 5172067 13051009 7428578 12169200 10025199 889023 5682300 19466758
8266664 6461146 8469097.8 21361926 12169200 20193512 16460646 1427838 9345008 31939277
6896582 5369522 7077253.8 18147704 10025199 16460646 13994854 1214314 7802940 26672047
592885 460691 660062.25 1658090 889023 1427838 1214314 271810 674289 2416415
4010500 3110916 4054182.5 10135618 5682300 9345008 7802940 674289 4543486 15207651
13442229 10472891 13845287 34679957 19466758 31939277 26672047 2416415 15207651 54044924

Example 14 (Horizontal concatenation in SAS/IML)¶

In [17]:
options nodate nonumber;
proc iml;
    M1 = {1 2 3,4 5 6, 7 8 9} ;  
    M2 = {7 8 9, 4 5 6, 1 2 3} ;  
    H_concat= M1 || M2;  
    print M1 M2 H_concat;
quit;
Out[17]:
SAS Output

The SAS System

M1   M2   H_concat  
1 2 3 7 8 9 1 2 3 7 8 9
4 5 6 4 5 6 4 5 6 4 5 6
7 8 9 1 2 3 7 8 9 1 2 3

Example 15 (Vertical concatenation in SAS/IML)¶

In [105]:
ods graphics off; 
options nodate nonumber;
proc iml;
  M1 = {1 2 3,4 5 6, 7 8 9} ;  
  M2 = {7 8 9, 4 5 6, 1 2 3};  
  V_concat= M1 // M2;  
  print M1 M2 V_concat;
quit;
SAS Output

The SAS System

M1   M2   V_concat  
1 2 3 7 8 9 1 2 3
4 5 6 4 5 6 4 5 6
7 8 9 1 2 3 7 8 9
            7 8 9
            4 5 6
            1 2 3

Contributed by Ksharp to SAS-L 04/08/2018 (Comments on Wicklin 's Code)

Example 16 (Contingency table analysis in SAS/IML)¶

In [33]:
proc iml;
    cName = {"black" "dark" "fair" "medium" "red"};
    rName = {"blue" "brown" "green"};

    C = { 6 51 69 68 28,
         16 94 90 94 47,
          0 37 69 55 38};

    total = C[+];                 /* Grand total */

    /* marginal probabilities */
    colMarg = C[+, ] / total;     /* column proportions */
    rowMarg = C[ ,+] / total;     /* row proportions */

    /* expected counts under independence */
    expect = (rowMarg * colMarg) # total;

    print C colMarg rowMarg;
    print expect[r=rName c=cName];
quit;
SAS Output

The SAS System

C   colMarg   rowMarg
6 51 69 68 28 0.0288714 0.2388451 0.2992126 0.2847769 0.148294 0.2913386
16 94 90 94 47           0.4475066
0 37 69 55 38           0.2611549
expect
  black dark fair medium red
blue 6.4094488 53.023622 66.425197 63.220472 32.92126
brown 9.8451444 81.446194 102.0315 97.108924 50.568241
green 5.7454068 47.530184 59.543307 56.670604 29.510499

Example 17 (Creating and manipulating basic vectors (row, column, reversed, and character)¶

In [20]:
options nocenter nodate nonumber;
options nocenter nodate nonumber;

proc iml;
    rv = 1:3;              /* row vector */
    cv = t(1:3);           /* column vector */
    reverse_rv = 3:1;      /* reverse row vector */

    Char_vec = {"Day1" "Day2" "Day3" "Day4" "Day5" "Day6" "Day7"};

    print rv cv reverse_rv Char_vec;
quit;
Out[20]:
SAS Output

The SAS System

rv   cv reverse_rv   Char_vec  
1 2 3 1 3 2 1 Day1 Day2 Day3 Day4 Day5 Day6 Day7
      2                    
      3                    

Example 18 (Creating a 5 × 5 identity matrix in SAS/IML)¶

options nocenter nodate nonumber; proc iml; I_mat=I(5); *5x5 identity matrix; print i_mat; quit;

Example 19 (Using the DO function to create a numeric sequence)¶

In [22]:
options nocenter nodate nonumber;
proc iml;
 /*... arithmetic series with some increment*/
series= do(5,50,10); 
print series;
quit;
Out[22]:
SAS Output

The SAS System

series
5 15 25 35 45

Example 20 (Basic vector operations and construction of a constant matrix using J() function)¶

In [23]:
options nocenter nodate nonumber;
proc iml;
 obs= {8 4 4 3 6 11};
 n=sum(obs);
 ncol_obs = ncol(obs);
 *constant matrix - J(nrow,ncol,value);
 p=j(1, ncol_obs, 1/ncol_obs);   
 np=n*p;
 print obs n ncol_obs p np;
quit;
Out[23]:
SAS Output

The SAS System

obs   n ncol_obs
8 4 4 3 6 11 36 6
p   np  
0.1666667 0.1666667 0.1666667 0.1666667 0.1666667 0.1666667 6 6 6 6 6 6

Example 21 (Using the J() function in SAS/IML)¶

In [24]:
options nocenter nodate nonumber;
proc iml;
/*Creating a  matrix with a J function 
 5 X 1 column vector of 1's*/
J_b=j(5,1);      /*5 X 1 column vector of 1's*/
print J_b;
quit;
Out[24]:
SAS Output

The SAS System

J_b
1
1
1
1
1

Example 22 (Using a matrix extraction function)¶

In [25]:
options nocenter nodate nonumber;
proc iml;
    X={1 2 3, 4 5 6, 7 8 9};
    vec_diag = vecdiag(X);
    print X vec_diag;
quit;
Out[25]:
SAS Output

The SAS System

X   vec_diag
1 2 3 1
4 5 6 5
7 8 9 9

Example 23 (Transposing a matrix)¶

In [26]:
*Ex22_transposing_matrix.sas;
options nocenter nodate nonumber;
proc iml;
  m = {1 2, 3 4, 5 6};
  T_m = t(m) ;  
print m T_m;
quit;
Out[26]:
SAS Output

The SAS System

m   T_m  
1 2 1 3 5
3 4 2 4 6
5 6      

Example 24 (Inverting a matrix)¶

In [46]:
options nocenter nodate nonumber;
proc iml;
    m = {7 1 2, 3 8 5, 6 7 8};
   I_m = inv(m) ;  
print m I_m;
quit;
SAS Output

The SAS System

m   I_m  
7 1 2 0.1870968 0.0387097 -0.070968
3 8 5 0.0387097 0.283871 -0.187097
6 7 8 -0.174194 -0.277419 0.3419355

Example 25 (Using the SAS/IML REPEAT function)¶

In [50]:
options nocenter nodate nonumber;
proc iml;
   X={1 2, 0 4}; 
   repeat_x22= repeat(X, 3, 2);
print X repeat_x22;
quit;
SAS Output

The SAS System

X   repeat_x22  
1 2 1 2 1 2
0 4 0 4 0 4
    1 2 1 2
    0 4 0 4
    1 2 1 2
    0 4 0 4

Example 26 (Basic matrix creation and simple matrix/summary operations in SAS/IML)¶

In [56]:
options nocenter nodate nonumber;

proc iml;
    X = {1 2 3,
         4 5 6,
         7 8 9};

    Y = {7 1 2,
         3 8 5,
         6 7 8};

    sum_X_Y = sum(X) + sum(Y);   /* correct interpretation */
    max_X    = max(X);
    ncol_Y   = ncol(Y);
    nrow_Y   = nrow(Y);

    print X Y sum_X_Y max_X ncol_Y nrow_Y;
quit;
SAS Output

The SAS System

X   Y   sum_X_Y max_X ncol_Y nrow_Y
1 2 3 7 1 2 92 9 3 3
4 5 6 3 8 5        
7 8 9 6 7 8        

Example 27 (Using a vectorized function: CHOOSE)¶

In [30]:
*Ex26_Choose1.sas (Part 1);
proc iml;
id = {1,2,3,4,5};
quiz1= {12,18,13,9,7};
makeup={10, 17, 17, 12, 13};
r_quiz1=choose(makeup>quiz1, makeup, quiz1);
print  id quiz1 makeup r_quiz1;
quit;
Out[30]:
SAS Output

The SAS System

id quiz1 makeup r_quiz1
1 12 10 12
2 18 17 18
3 13 17 17
4 9 12 12
5 7 13 13

Example 28 - Performing an elective replacement of missing values using the CHOOSE().¶

In [31]:
*Acknowledgements: Xia Kesan SAS-L;
proc iml;
 faulty = {15   12   13,    
           16   29   13,
           15   12   13,
           18   58   11 };
 updates = {.   .    .,       
           16   29   .,
            .   .    .,
           18   58   .};
 want=choose(updates^=.,updates,faulty );
 print want;
 quit;
Out[31]:
SAS Output

The SAS System

want
15 12 13
16 29 13
15 12 13
18 58 11

Example 29 (Column-wise aggregation + maximum selection pattern in SAS/IML)¶

In [38]:
*Maximum of the column totals;
ods html;
proc iml;
A = {1 2 3, 4 5 6, 9 8 7, 3 2 1, 5 4 2};
max_c_sum= A[+, <>];
print A, max_c_sum ;
quit;
Out[38]:
SAS Output

The SAS System

A
1 2 3
4 5 6
9 8 7
3 2 1
5 4 2
max_c_sum
22

Example 30¶

  • Compute the column sum

  • Minimum column sum

  • column sums = {22 21 19}

  • minimum = 19

In [61]:
proc iml;
A = {1 2 3,
     4 5 6,
     9 8 7,
     3 2 1,
     5 4 2};

colsum = A[+, ];        /* column sums */
min_c_sum = colsum[><]; /* minimum of column sums */

print A colsum min_c_sum;
quit;
SAS Output

The SAS System

A   colsum   min_c_sum
1 2 3 22 21 19 19
4 5 6        
9 8 7        
3 2 1        
5 4 2        

Example 31 - Getting the column-wise maximum¶

  • Column-wise maxima:
    • Column 1 → max = 9
    • Column 2 → max = 8
    • Column 3 → max = 7
In [66]:
*Index of the column maxima;
ods html close;

proc iml;
A = {1 2 3,
     4 5 6,
     9 8 7,
     3 2 1,
     5 4 2};

i_col_maxima = A[<>, ];   /* column-wise maximum */

print A i_col_maxima;
quit;
SAS Output

The SAS System

A   i_col_maxima  
1 2 3 9 8 7
4 5 6      
9 8 7      
3 2 1      
5 4 2      

Example 32 - Getting the column-wise minimum values and their indices in a matrix using SAS/IML¶

  • For each column of matrix A, find the row number where the smallest value occurs.

  • col is assumed to be a vector (typically a column of a matrix)

  • Symbol >< is the minimum reduction operator

  • col[><] = smallest value in col

In [72]:
ods html close;

proc iml;
A = {1 2 3,
     4 5 6,
     9 8 7,
     3 4 1,
     5 7 2};

ncol = ncol(A);
idx = j(1, ncol, .);

do j = 1 to ncol(A);
    col = A[, j];
    minval = col[><];
    idx[j] = loc(col = minval);  /* row index of min */
end;

print A idx[colname=("Col1":"Col3")];
quit;
SAS Output

The SAS System

A   idx
Col1
Col2 Col3
1 2 3 1 1 4
4 5 6      
9 8 7      
3 4 1      
5 7 2      

Example 33 - Finding the maximum value in each row of matrix A, then sum those maximum values¶

In [40]:
*Sum of the rowwise maximum values;
ods html;
proc iml;
A = {1 2 3, 4 5 6, 9 8 7, 3 2 1, 5 4 2};
sum_max_r=A[,<>] [+, ];
print A ,sum_max_r ;
quit;
Out[40]:
SAS Output

The SAS System

A
1 2 3
4 5 6
9 8 7
3 2 1
5 4 2
sum_max_r
26

Example 34: Computing the total of each of the first two columns of matrix A.¶

In [82]:
*Totals of selected columns;
ods html;
proc iml;
A = {1 2 3, 4 5 6, 9 8 7, 3 2 1, 5 4 2};
sum_c12 = A[+, 1:2];
print A, sum_c12 ;
quit;
SAS Output

The SAS System

A
1 2 3
4 5 6
9 8 7
3 2 1
5 4 2
sum_c12
22 21

Example 35 - Finding the maximum value of a submatrix¶

  • The code extracts the submatrix consisting of rows 1–2 and columns 1–3 of A, and then finds the maximum value in that submatrix.
In [85]:
* Maximum value of the row-column dimension specified; 
proc iml;
A = {1 2 3, 4 5 6, 9 8 7, 3 2 1, 5 4 2};
max_row_1_2_col_1_3 = A[1:2, 1:3] [<>];
print A, max_row_1_2_col_1_3 ;
quit;
SAS Output

The SAS System

A
1 2 3
4 5 6
9 8 7
3 2 1
5 4 2
max_row_1_2_col_1_3
6

Example 36 - Finding the minimum value of a submatrix¶

In [43]:
* Minimum value of the row-column dimension specified; 
ods html;
proc iml;
A = {1 2 3, 4 5 6, 9 8 7, 3 2 1, 5 4 2};
min_row_1_2_col_1_3 = A[1:2, 1:3] [><];
print A, min_row_1_2_col_1_3; 
quit;
Out[43]:
SAS Output

The SAS System

A
1 2 3
4 5 6
9 8 7
3 2 1
5 4 2
min_row_1_2_col_1_3
1

Example 37 - Computing the column-wise mean of matrix¶

In [44]:
proc iml;
A = {1 2 3,
     4 5 6,
     9 8 7,
     3 2 1,
     5 4 2};

mean_1_2_3 = A[:,];   /* column means */

print A mean_1_2_3;
quit;
Out[44]:
SAS Output

The SAS System

A
1 2 3
4 5 6
9 8 7
3 2 1
5 4 2
mean_1_2_3
4.4 4.2 3.8
In SAS/IML:

Expression	Meaning
A[:,]	column-wise means
A[+,]	column sums
A[, +]	row sums
A[<>,]	column maxima
A[><,]	column minima
````

Example 38¶

Extracting male observations from SASHELP.CLASS, converting them into a labeled matrix, and storing it permanently in a SAS library.¶
  • Set a custom line-drawing format (FORMCHAR)
  • Define a library path (imlin)
  • Read male observations from SASHELP.CLASS
  • Extract:
    • numeric variables → matrix
    • names → row labels
    • Assign column names
    • Store matrix in a permanent library
    • Print with labels
In [135]:
OPTIONS FORMCHAR="|----|+|---+=|-/\<>*";
%LET Path=C:\users\pmuhuri\SASCourse\Week12;
libname imlin "&Path";

PROC IML;
reset deflib=imlin;

/* Read numeric variables for males */
use sashelp.class;
read all var _num_ into class_male_nummat_ext
    where(sex="M");

/* Read names for row labels */
read all var {name} into rows
    where(sex="M");

cols = {"Age" "Height" "Weight"};

/* Store matrix permanently */
reset storage=imlin.Mymat;
store class_male_nummat_ext;

/* Print with labels */
print class_male_nummat_ext[colname=cols rowname=rows];

close;
quit;
SAS Output

The SAS System

class_male_nummat_ext
  Age Height Weight
Alfred 14 69 112.5
Henry 14 63.5 102.5
James 12 57.3 83
Jeffrey 13 62.5 84
John 12 59 99.5
Philip 16 72 150
Robert 12 64.8 128
Ronald 15 67 133
Thomas 11 57.5 85
William 15 66.5 112

Example 39 Converting an IML matrix into a SAS dataset with labeled columns.¶

In [47]:
proc iml;

a = {11 22 33,
     44 55 66,
     77 88 99,
     33 22 21};

/* Create SAS dataset from matrix */
create data_from_matrix 
    from a[colname={"Test1" "Test2" "Test3"}];

append from a;
close data_from_matrix;

/* Print matrix */
print a;

quit;
Out[47]:
SAS Output

The SAS System

a
11 22 33
44 55 66
77 88 99
33 22 21

   OBS     Test1     Test2     Test3                                                                                                

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

     1   11.0000   22.0000   33.0000                                                                                                

     2   44.0000   55.0000   66.0000                                                                                                

     3   77.0000   88.0000   99.0000                                                                                                

     4   33.0000   22.0000   21.0000                                                                                                

                                                                                                                                    

Example 40 - Counting missing values across multiple variables by using an array and CMISS.¶

In [48]:
data Missing;
input A B C;
datalines;
2 1 1
4 . .
1 3 1
. 6 1
. 1 .
3 4 2
;
data A;
set Missing;
array vars(3) A--C; 
numMissing = cmiss(of vars[*]);
run;
proc print data=A; run;
Out[48]:
SAS Output

The SAS System

Obs A B C numMissing
1 2 1 1 0
2 4 . . 2
3 1 3 1 0
4 . 6 1 1
5 . 1 . 2
6 3 4 2 0

Example 41: Doing row-wise missing value detection and filtering¶

  • Just code, no instream data below
In [ ]:
proc iml;

use Missing;
read all var _NUM_ into x;
close Missing;

/* row-wise missing count */
missInd = (x = .);
rowMiss = missInd[,+];

print x rowMiss;

/* indices */
jdx = loc(rowMiss > 0);
idx = loc(rowMiss = 0);

print jdx;

y = x[idx,];
print y;

quit;

Example 42 - Sorting a dataset, analyzing population data, and computing summary statistics in IML¶

In [146]:
proc iml;
 sort Sashelp.demographics out=Sorted_countries 
    by descending pop;
 varnames = {'Name', 'Pop'};
 use Sorted_countries;
 read all var varnames; 
 close sashelp.demographics;
 idx = loc(pop>140000000);

 mean_world_pop1=pop[:];
 mean_world_pop2=mean(pop);
 mean_world_pop3=sum(pop)/nrow(pop);
 Number_of_countries=nrow(name);
 print (name[idx]) 
       (pop[idx])[format=comma15.];
  print  
   Number_of_countries,
   mean_world_pop1 [format=comma15.],
   mean_world_pop2 [format=comma15.],
   mean_world_pop3 [format=comma15.];
 quit;
SAS Output

The SAS System

CHINA 1,323,344,591
INDIA 1,103,370,802
UNITED STATES 298,212,895
INDONESIA 222,781,487
BRAZIL 186,404,913
PAKISTAN 157,935,075
RUSSIA 143,201,572
BANGLADESH 141,822,276
Number_of_countries
197
mean_world_pop1
33,870,294
mean_world_pop2
33,870,294
mean_world_pop3
33,870,294

Example 43¶

  • Data sorting, filtering, and statistical analysis entirely inside PROC IML using vectorized matrix operations—without any PROC SORT or DATA step dependency
In [151]:
proc iml;
  use sashelp.demographics;
 read all var {pop}  
      into x[rowname=name colname=pop];
 close sashelp.demographics;
 pop_India_over_china=(x['India', 'pop']/x['China', 'pop'])-1;
 print pop_India_over_china[format=percent7.2];
 quit;
SAS Output

The SAS System

pop_India_over_china
(16.6%)

Finding matrix elements that satisfy a logical expression by Rick Wicklin

Example 44¶

In [158]:
* Code by Rik Wicklin;
proc iml;
x = {. -5 2  5,
    -2  . 3  4,
     4  . . -1};
missingLoc = loc(x=.);             /* missing values */
negativeLoc = loc(x^=. & x<0);     /* nonmissing and negative  */
evenLoc = loc(mod(x,2)=0);         /* n is even if (n mod 2)=0 */
print missingLoc negativeLoc evenLoc;
SAS Output

The SAS System

missingLoc   negativeLoc   evenLoc  
1 6 10 11 2 5 12 3 5 8 9

Example 45: Storing a Matrix into the Default Library¶

The STORE statement saves the matrix MYMAT to storage in the work library of the defaults library.

In [205]:
PROC IML;
  USE sashelp.class;
  READ all var _num_ INTO Mymat; 
  store Mymat;                        
  CLOSE sashelp.class;
QUIT;
The SAS System

NOTE: Writing HTML5(SASPY_INTERNAL) Body file: _TOMODS1
NOTE: IML Ready
NOTE: Opening storage library WORK.IMLSTOR
NOTE: Exiting IML.
NOTE: Storage library WORK.IMLSTOR closed.
NOTE: PROCEDURE IML used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds
      

The SAS System

E3969440A681A2408885998500000207

Example 46: Loading and Printing the Matrix from the Default Library¶

The LOAD statement recalls entry from back into IML workplace from storage.

The PRINT statement prints the matrix MYMAT.

In [56]:
*Ex34_store_load_save_mat.sas (Part 2);
PROC IML;
 LOAD Mymat;
 PRINT Mymat;
 QUIT;
Out[56]:
SAS Output

The SAS System

Mymat
14 69 112.5
13 56.5 84
13 65.3 98
14 62.8 102.5
14 63.5 102.5
12 57.3 83
12 59.8 84.5
15 62.5 112.5
13 62.5 84
12 59 99.5
11 51.3 50.5
14 64.3 90
12 56.3 77
15 66.5 112
16 72 150
12 64.8 128
15 67 133
11 57.5 85
15 66.5 112

Example 47: Saving Matrices (or Modules) Permanently¶

The RESET statement with DEFLIB = operand is used to specify the library name.

The RESET STORAGE statement is used to specify both library reference and catalog.

The STORE statement saves the matrix MYMAT2 in the catalog named CAT1 in the IMLIN library.

In [164]:
*Ex34_store_load_save_mat.sas (Part 3);
libname imlin "C:/users/pmuhuri/SASCourse/Week12";
PROC IML;
reset deflib=imlin;
  USE sashelp.class;
  READ all var _num_ INTO Mymat2; 
  reset storage=imlin.cat1;
  store Mymat2;
  show storage;
  CLOSE sashelp.class;
QUIT;
SAS Output

The SAS System


Contents of storage library = IMLIN.CAT1                                                                                            

                                                                                                                                    

Matrices:                                                                                                                           

MYMAT2                                                                                                                              

                                                                                                                                    

Modules:                                                                                                                            

                                                                                                                                    

Example 48: RESET and LOAD statements¶

The RESET statement with DEFLIB = operand is used to specify the library name.

The RESET STORAGE statement is used to specify both library reference and catalog.

The LOAD statement recalls the matrix MYMAT2 that was earlier saved permanently in the catalog named CAT1 in the IMLIN library.

In [183]:
libname imlin "C:\users\pmuhuri\SASCourse\Week12";
PROC IML;
 RESET deflib=imlin;
 RESET STORAGE=imlin.cat1;
 LOAD Mymat2;
 PRINT Mymat2;
 QUIT;
SAS Output

The SAS System

Mymat2
14 69 112.5
13 56.5 84
13 65.3 98
14 62.8 102.5
14 63.5 102.5
12 57.3 83
12 59.8 84.5
15 62.5 112.5
13 62.5 84
12 59 99.5
11 51.3 50.5
14 64.3 90
12 56.3 77
15 66.5 112
16 72 150
12 64.8 128
15 67 133
11 57.5 85
15 66.5 112

Example 49: The DATASETS function returns the names of all SAS data sets in a specified libref.¶

In [187]:
PROC IML;
  USE sashelp.class;
  READ all var _num_ INTO c_m_nummat
        where(sex='M');
  CLOSE sashelp.class;
  PRINT c_m_nummat;
QUIT;
SAS Output

The SAS System

c_m_nummat
14 69 112.5
14 63.5 102.5
12 57.3 83
13 62.5 84
12 59 99.5
16 72 150
12 64.8 128
15 67 133
11 57.5 85
15 66.5 112

Example 50¶

In [70]:
PROC IML;
  USE sashelp.class;
  READ all var _num_ INTO nummat  where(sex='M');
  READ all var {name} INTO charmat where(sex='M');
         cols = {'Age' 'Height' 'Weight'};
  CLOSE sashelp.class;
  PRINT nummat [rowname=charmat 
                colname=cols
                label= ' '
                format=5.0];
QUIT;
Out[70]:
SAS Output

The SAS System

 
  Age Height Weight
Alfred 14 69 113
Henry 14 64 103
James 12 57 83
Jeffrey 13 63 84
John 12 59 100
Philip 16 72 150
Robert 12 65 128
Ronald 15 67 133
Thomas 11 58 85
William 15 67 112

Example 51¶

In [71]:
PROC IML;
  USE sashelp.class;
  READ all var _num_ where(sex='M');
  CLOSE sashelp.class;
  PRINT age height weight;
QUIT;
Out[71]:
SAS Output

The SAS System

Age Height Weight
14 69 112.5
14 63.5 102.5
12 57.3 83
13 62.5 84
12 59 99.5
16 72 150
12 64.8 128
15 67 133
11 57.5 85
15 66.5 112
Example 52¶
In [72]:
PROC IML;
  USE sashelp.class where(name=:'J');
  READ all var {name sex} INTO class_J_mat;
  CLOSE sashelp.class;
QUIT;
Out[72]:

The SAS System

NOTE: Writing HTML5(SASPY_INTERNAL) Body file: _TOMODS1
NOTE: IML Ready
NOTE: Exiting IML.
NOTE: PROCEDURE IML used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds


E3969440A681A2408885998500000074

Example 53¶

In [189]:
PROC IML;
  USE sashelp.class;
  READ all var _num_ INTO num_mat;
  READ all var _char_ INTO char_mat;
  CLOSE sashelp.class;
  PRINT num_mat char_mat;
QUIT;
SAS Output

The SAS System

num_mat   char_mat  
14 69 112.5 Alfred M
13 56.5 84 Alice F
13 65.3 98 Barbara F
14 62.8 102.5 Carol F
14 63.5 102.5 Henry M
12 57.3 83 James M
12 59.8 84.5 Jane F
15 62.5 112.5 Janet F
13 62.5 84 Jeffrey M
12 59 99.5 John M
11 51.3 50.5 Joyce F
14 64.3 90 Judy F
12 56.3 77 Louise F
15 66.5 112 Mary F
16 72 150 Philip M
12 64.8 128 Robert M
15 67 133 Ronald M
11 57.5 85 Thomas M
15 66.5 112 William M

The XSECT function returns as a row vector the sorted set (without duplicates) of the element values that are present in all of its arguments. This set is the intersection of the sets of values in its argument matrices.

When the intersection is empty, the XSECT function returns an empty matrix with zero rows and zero columns. There can be up to 15 arguments, which must all be either character or numeric.” SAS® Documentation.

The SETDIF function returns as a row vector the sorted set (without duplicates) of all element values present in A but not in B. If the resulting set is empty, the SETDIF function returns an empty matrix with zero rows and zero columns. SAS® Documentation.

In [191]:
*Ex38_XSECT_SETDIF.sas (Part 1);
* Contributed by Rick Wicklin to SAS-L - 12/28/2016;
proc iml;
candidates = {Name, Sex, Income};
varNames = contents("sashelp","class");
In = xsect(upcase(candidates), upcase(varNames));
Out = setdif(upcase(candidates), upcase(varNames));
print In[label="Vars In Data"], Out[label="Vars Not In Data"];
quit;
SAS Output

The SAS System

Vars In Data
NAME SEX
Vars Not In Data
INCOME

Example 54¶

In [193]:
proc iml;
varNames_d1 = contents("sashelp","class");;
varNames_d2 = contents("sashelp","classfit");
In = xsect(upcase(varNames_d1), upcase(varNames_d2));
Out = setdif(upcase(varNames_d1), upcase(varNames_d2));
*print In[label="Vars In Data"], Out[label="Vars Not In Data"];
nrow_d1=nrow(varNames_d1);
nrow_d2=nrow(varNames_d2);
print varNames_d1 varNames_d2, nrow_d1 nrow_d2;
quit;
SAS Output

The SAS System

varNames_d1 varNames_d2
Name Name
Sex Sex
Age Age
Height Height
Weight Weight
  predict
  lowermean
  uppermean
  lower
  upper
nrow_d1 nrow_d2
5 10

Example 55¶

In [77]:
OPTIONS nofmterr FORMCHAR="|----|+|---+=|-/\<>*";
libname imlin "C:\SASCourse\Week12";
 options fmtsearch=(imlin.formats);
proc iml;
reset deflib=imlin;
 use imlin.for_iml_pop2013 var {State_Name Pop}; 
 show contents;
close imlin.for_iml_pop2013;
edit imlin.for_iml_pop2013 
  var {State_FIPS State_Name Pop} 
   where(State_FIPS <=8);
list all;
var_group = {State_Name Pop};
list all var var_group 
   where(State_FIPS <=4);
show contents;
show datasets;
quit;
Out[77]:
SAS Output

The SAS System


DATASET : IMLIN.FOR_IML_POP2013.DATA                                                                                                

                                                                                                                                    

 VARIABLE                          TYPE  SIZE                                                                                       

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

 State_Name                        char    22                                                                                       

 Pop                               num      8                                                                                       

                                                                                                                                    

Number of Variables   : 2                                                                                                           

Number of Observations: 51                                                                                                          

                                                                                                                                    


   OBS State_FIPS State_Name                      Pop                                                                               

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

     1 01         Alabama                   4,833,722                                                                               

     2 02         Alaska                      735,132                                                                               

     3 04         Arizona                   6,626,624                                                                               

     4 05         Arkansas                  2,959,373                                                                               

     5 06         California               38,332,521                                                                               

     6 08         Colorado                  5,268,367                                                                               

                                                                                                                                    


   OBS State_Name                      Pop                                                                                          

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

     1 Alabama                   4,833,722                                                                                          

     2 Alaska                      735,132                                                                                          

     3 Arizona                   6,626,624                                                                                          

                                                                                                                                    


DATASET : IMLIN.FOR_IML_POP2013.DATA                                                                                                

                                                                                                                                    

 VARIABLE                          TYPE  SIZE                                                                                       

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

 State_FIPS                        num      8                                                                                       

 State_Name                        char    22                                                                                       

 Pop                               num      8                                                                                       

                                                                                                                                    

Number of Variables   : 3                                                                                                           

Number of Observations: 51                                                                                                          

                                                                                                                                    


LIBNAME  MEMNAME                          OPEN MODE   STATUS                                                                        

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

IMLIN    FOR_IML_POP2013                  Update      Current Input/Output                                                          

                                                                                                                                    

Example 56¶

In [195]:
OPTIONS nocenter ps=58 ls=72 nodate nonumber 
  FORMCHAR="|----|+|---+=|-/\<>*" ;
proc iml;
 use sashelp.class ;
 *list all;
 list point 3;
 list point {2 4};
 p= {1 3 5};
 v={name height weight};
 list point p var v;
 list all var v where (weight >=150);
quit;
SAS Output

The SAS System


   OBS Name     Sex       Age    Height    Weight                       

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

     3 Barbara  F     13.0000   65.3000   98.0000                       

                                                                        


   OBS Name     Sex       Age    Height    Weight                       

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

     2 Alice    F     13.0000   56.5000   84.0000                       

     4 Carol    F     14.0000   62.8000  102.5000                       

                                                                        


   OBS Name        Height    Weight                                     

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

     1 Alfred     69.0000  112.5000                                     

     3 Barbara    65.3000   98.0000                                     

     5 Henry      63.5000  102.5000                                     

                                                                        


   OBS Name        Height    Weight                                     

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

    15 Philip     72.0000  150.0000                                     

                                                                        

Example 57¶

In [197]:
options nodate nonumber;
proc iml;
USE sashelp.heart;
 SUMMARY class {sex} var {AgeAtDeath  weight};
CLOSE;
quit;
SAS Output

The SAS System


Sex     Nobs  Variable           MIN         MAX        MEAN         STD

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

Female  2873  AgeAtDeath    36.00000    93.00000    71.56696    10.83126

              Weight        67.00000   300.00000   141.38864    26.28804

                                                                        

Male    2336  AgeAtDeath    36.00000    91.00000    69.69315    10.25983

              Weight        99.00000   276.00000   167.46615    25.29070

                                                                        

All     5209  AgeAtDeath    36.00000    93.00000    70.53641    10.55941

              Weight        67.00000   300.00000   153.08668    28.91543

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

                                                                        

Example 58¶

In [199]:
proc iml;
USE sashelp.heart;
summary class {sex} var {AgeAtDeath weight} 
        stat {mean std var} opt {noprint save};
CLOSE;
show names;
print AgeAtDeath[r=sex c={"Mean" "Std"} format=5.1], 
      weight[r=sex c={"Mean" "Std"} format=5.1];
quit;
SAS Output

The SAS System


 SYMBOL           ROWS   COLS TYPE   SIZE                               

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

 AgeAtDeath          2      3 num       8                               

 Sex                 2      1 char      6                               

 Weight              2      3 num       8                               

 _NOBS_              2      1 num       8                               

  Number of symbols = 18  (includes those without values)               

                                                                        

AgeAtDeath Mean Std  
Female 71.6 10.8 117.3
Male 69.7 10.3 105.3
Weight Mean Std  
Female 141.4 26.3 691.1
Male 167.5 25.3 639.6

Example 59¶

In [85]:
*Ex42_update.sas;
data master;
 input id quiz1 @@;
 datalines;
 1 12 2 18 3 13 4 9 5 7
 ;
 title1 'Master Data File';
 proc print data=master noobs; run;
 
data transact;
 input id quiz1  @@;
 datalines;
 3 17 4 12 5 13
; 
 title1 'Transaction  Data File';
 
 proc print data=transact noobs; run;
 
data updated; 
  UPDATE master transact; 
   by ID;
run;
 title1 'Updated Data File';
proc print data=updated noobs; 
run;
title1;
Out[85]:
SAS Output

Master Data File

id quiz1
1 12
2 18
3 13
4 9
5 7

Transaction Data File

id quiz1
3 17
4 12
5 13

Updated Data File

id quiz1
1 12
2 18
3 17
4 12
5 13

Example 60¶

In [203]:
*R_List_of_Files.sas;
title;
PROC IML;
SUBMIT / R;
setwd ("C:/users/pmuhuri/SASCourse/Week12/SAS_Codes")
list.files(pattern="SAS*", full.names = TRUE, ignore.case = TRUE) 
ENDSUBMIT;
QUIT;
SAS Output

 [1] "./cat1.sas7bcat"                                                  

 [2] "./Ex_simulate_data_logistic_reg_models.sas"                       

 [3] "./Ex1_creating_Vectors_Matrices.sas"                              

 [4] "./Ex10_Matrix_product.sas"                                        

 [5] "./Ex11_Matrix_division.sas"                                       

 [6] "./Ex12_Matrix_Power.sas"                                          

 [7] "./Ex13_Horizontal_Concat.sas"                                     

 [8] "./Ex14_Vertical_Concat.sas"                                       

 [9] "./Ex15_marg_prob.sas"                                             

[10] "./Ex16_Create_matrices_op_func.sas"                               

[11] "./Ex17_Create_identity_matrix.sas"                                

[12] "./Ex18_create_matrix_series.sas"                                  

[13] "./Ex19_Create_matrix_J_Function.sas"                              

[14] "./Ex2_matrix_addition.sas"                                        

[15] "./Ex20_Create_matrix_J_Function2.sas"                             

[16] "./Ex21_Create_diagoanal_matrix.sas"                               

[17] "./Ex22_R_List_of_Files.sas"                                       

[18] "./Ex22_transposing_matrix.sas"                                    

[19] "./Ex23_inverting_matrix.sas"                                      

[20] "./Ex24_Repeat_matrix.sas;.sas"                                    

[21] "./Ex25_matrix_functions.sas"                                      

[22] "./Ex26_choose1.sas"                                               

[23] "./Ex27_Reduction_Operators.sas"                                   

[24] "./Ex28_store_matrix.sas"                                          

[25] "./Ex3_matrix_subtraction.sas"                                     

[26] "./Ex30_create_SDS_from_matrix.sas"                                

[27] "./Ex31_CMISS_Base_IML.sas"                                        

[28] "./Ex32_loc.sas"                                                   

[29] "./Ex33_IML_LOC_RickW.sas"                                         

[30] "./Ex34_store_load_save_mat.sas"                                   

[31] "./Ex35_Select_Top_Bottom_Values.sas"                              

[32] "./Ex36_DATASETS Func.sas"                                         

[33] "./Ex37_create_mats_from_SDS.sas"                                  

[34] "./Ex38_XSECT_SETDIF.sas"                                          

[35] "./Ex39_USE_EDIT_Statements.SAS"                                   

[36] "./Ex4_Matrix_Elwise_Multi.sas"                                    

[37] "./Ex40_USE_LIST_POINT_VAR_WHERE.sas"                              

[38] "./Ex41_Summary.sas"                                               

[39] "./Ex42_update.sas"                                                

[40] "./Ex5_Matrix_Elwise_Power.sas"                                    

[41] "./Ex6_Matrix_Elwise_sr.sas"                                       

[42] "./Ex7_Matrix_scalar_add.sas"                                      

[43] "./Ex8_Matrix_scalar_subtract.sas"                                 

[44] "./Ex9_Matrix_scalar_div.sas"                                      

[45] "./for_iml_pop2013.sas7bdat"                                       

[46] "./Logistic.sas"                                                   

[47] "./Logistic_model.sas"                                             

[48] "./mymat.sas7bcat"                                                 

[49] "./R_List_of_Files.sas"                                            

[50] "./Run_R_using_IML.sas"