0

Am I right that, to add a new column to an existing SAS dataset, I use the Alter Table statement (and, for populating the new column, the Update statement) in proc sql? If so, what are the equivalent statements in proc iml (or can it even be done in IML)?

Mhoram
  • 421
  • 2
  • 6
  • 13
  • IML doesn't really have a concept of 'dataset', it has a matrix. Can you post an example of what you're asking? And, right now you're asking more than one question. Are you specifically asking the IML question and the rest is just extraneous information? – Joe Nov 05 '15 at 18:02
  • Personally I wouldn't use ALTER table and Update to add a variable to a table. I would make a new table that contains the variables I want from the old table and merges on or calculates the new variable. In IML I would do the same thing. Make a new matrix by concatenating the old matrix with a new matrix that had compatible dimensions. – Tom Nov 05 '15 at 23:30
  • How do I make sure that the values in the new table being concatenated line up with the corresponding values from the old dataset? – Mhoram Nov 06 '15 at 16:04

1 Answers1

0

It sounds like you are asking about horizontal concatentation. You can use the concatenation operator (||) to append the columns of one matrix to another, provided that both matrices have the same number of rows. For example, the following statement concatenate a 2x1 vector to a 2x3 matrix. The result is a 2x4 matrix:

proc iml;
x = {1 2 3,
     4 5 6};
y = {7, 8};
z = x || y;
print z;

Be aware, however, that this allocates a new matrix (z) and copies over the contents of the x and y matrices. Therefore it is not as efficient as creating a 4x2 matrix from the beginning and then using subscrits to fill the columns. For details, see the article "Friends don't let friends concatenate results inside a loop."

Rick
  • 1,210
  • 6
  • 11
  • What I am trying to do is to write the contents of a 1-column matrix into a new column in an existing dataset. Is there a way of doing this in IML (such that the new content lines up with the corresponding values in the dataset's existing columns)? – Mhoram Nov 06 '15 at 16:35
  • In SAS you need to create a new data set. You can't add a variable to an existing data set. – Rick Nov 08 '15 at 11:40