Extract and sample elements from SAS/IML vectors

0

If you want to extract values from a SAS/IML vector, use the subscripting operation, such as in the following example:

proc iml;
x = {A B C D E};
y = x[{1 2 3}];  /* {A,B,C} */

The vector y contains the first three elements of x. However, did you know that you can specify the indexes in any order?

z = x[{3 1 2}];  /* {C,A,B} */

The vector z contains the third, first, and second elements, in that order, of the vector x.

Furthermore, did you know that you can repeat indices to duplicate elements? And that you can extract many values?

w = x[{1 2 5 4 1 3 1 2}];  /* {A,B,E,D,A,C,A,B} */

This last example might cause you to wonder whether you can use this operation as a sampling method. Yes! This technique is one way to sample with replacement in the SAS/IML language. Sampling from x is easy: randomly generate N integer values between 1 and ncol(x), and use those values as indices to extract values from x:

N = 10; /* number of obs to sample */
r = j(1, N); /* allocate vector for indices */ 
call randgen(r, "uniform"); /* fill r with U[0,1] */
r = ceil(ncol(x)*r); /* create integers in [1, ncol(x)] */
y = x[r]; /* generate sample from x of size N */

This post is based on a tip from @RLangTip that I saw on Twitter. Many tips for doing something efficiently in the R language are equally applicable to other matrix-vector languages such as SAS/IML and MATLAB, and vice versa.

Share

About Author

Rick Wicklin

Distinguished Researcher in Computational Statistics

Rick Wicklin, PhD, is a distinguished researcher in computational statistics at SAS and is a principal developer of SAS/IML software. His areas of expertise include computational statistics, simulation, statistical graphics, and modern methods in statistical data analysis. Rick is author of the books Statistical Programming with SAS/IML Software and Simulating Data with SAS.

Leave A Reply

Back to Top