Reading and Writing Data |
Although this book does not require previous knowledge of SAS/IML, experienced users of IML might wonder how to transfer data between IML matrices and a DataObject. For completeness, this issue is addressed in this section.
The SAS/IML language provides statements to read and write server data to and from IML matrices. The USE statement opens a SAS data set, and the READ statement reads server data into IML matrices. IML also has a CREATE statement that creates a server data set and an APPEND statement that writes variables from IML matrices. These statements are documented fully in the SAS/IML User's Guide.
If you have data in IML matrices, you can create a DataObject from those matrices by using the Create method of the DataObject. For example, type the following statements into a Stat Studio program window, and select Program Run from the main menu.
xy = {0 4, 1 6, 2 7, 3 9, 4 10}; declare DataObject dobjMatrix; dobjMatrix = DataObject.Create("Matrix", {"XVar" "YVar"}, xy);
The matrix xy is used to initialize the dobjMatrix DataObject. The result is a DataObject with two variables (named XVar and YVar) and five observations.
z = {-1, 1, 0, 0, 1}; dobjMatrix.AddVar( "z", z); DataTable.Create( dobjMatrix );
The result is a DataObject with three variables (the new variable is z) and five observations. It is an error to try to add a new variable that has a different number of observations than the DataObject.
There is also a DataObject method, called the AddVars method, that enables you to add several variables in a single call.
dobjMatrix.GetVarData( "XVar", x); print x;
The result is a IML matrix x that contains the values of the XVar variable. You can name the IML matrix anything you want. It can have the same name as the variable in the DataObject (as the z variable and matrix did) or a completely different name (as in this example). After the data are copied into a matrix, there is no linkage between the DataObject and the IML matrix: changing elements of the matrix does not affect the DataObject, and changing the DataObject does not affect the matrix.
The CopyServerDataToDataObject module uses this approach to read variables in a server data set directly into a DataObject. This module is discussed in Chapter 5, "Adding Variables to the DataObject." Again, the number of observations in the server data set must match the number of observations in the DataObject.
Copyright © 2008 by SAS Institute Inc., Cary, NC, USA. All rights reserved.