Search code examples
matrixsas-imlelementwise-operations

Performing calculations on only certain columns in a matrix in SAS IML


I need to create, in IML, a matrix with several columns and do some calculations on only some columns (the values in one column must not change). For example, I'll need to multiply one column by another elementwise. What is the syntax for this?


Solution

  • You do this by referencing the column number you want to change on the left hand side of the equal sign.

    For example:

    proc iml;
      x = {1 2,
           3 0, 
           5 4};
      y = {1,
           2,
           3};
    
      x[,1] = x[,1] # y;
      print x;
    quit;
    

    As Rick noted in comments, you could also multiply column 1 of x with column 2:

    proc iml;
      x = {1 2,
           3 0, 
           5 4};
    
      x[,1] = x[,1] # x[,2];
      print x;
    quit;
    

    You may also want to look at the subscript reduction operators, as well as Rick's suggestions for shorthand notation for row and column operations for more information about related concepts.