I like to be efficient in my SAS/IML programs, but sometimes I get into bad habits. Recently I realized that I was reshaping a bunch of SAS/IML row vectors because I wanted to write them to a SAS data set.
This is completely unnecessary! The SAS/IML language will create a data set variable from any SAS/IML matrix, not matter what its shape. For example, suppose that you have a column vector, a row vector, and a matrix, and you want to write their data to a SAS data set, as follows:
proc iml; col = {1, 2, 3, 4, 5, 6}; /* 6 x 1 column vector */ row = 1:6; /* 1 x 6 row vector */ mat = {1 2 3, /* 2 x 3 matrix */ 4 5 6}; create MyData var {"col" "row" "mat"}; append; close MyData; quit; proc print data=MyData; run; |
See? The data are written as if all matrices are column vectors. For some reason I had started calling the SHAPE function or the COLVEC function to reshape the data before writing it:
/* These calls are UNNECESSARY! */ row = colvec(row); /* make column vector */ mat = shape(mat, 6, 1); /* make 6 x 1 vector */ |
I resolve to break this habit in 2013! Do you have a bad programming habit that you are hoping to correct in the New Year? Let me know in the comments.
1 Comment
I would say my bad habit is not having good habits.
My programs would be easier to deal with if I followed more conventions
things like
defining data sets at beginning of a program vs. where they are needed.
using %include appropriately
defining macros (mostly very short and easy macros) in some special place
etc