Reading and Writing Data |
As explained in Chapter 1, "Introduction," the DataObject class stores an in-memory version of data. You can query, retrieve, and manipulate the data by calling methods in the DataObject class. All graphical and tabular views of those data are linked together through the common DataObject. Therefore, when you want to create a graph of some data, or to look at the data in a table, you first need to create a DataObject.
A DataObject is typically created from a SAS data set. As explained in the introduction to this chapter, a client data set is accessible through the Windows operating environment, whereas a server data set resides in a SAS library.
In this section you write a program to create a DataObject from a SAS data set and save data from a DataObject to a SAS data set. You can open a program window by selecting File New Workspace from the main menu.
The program statements in this chapter are distributed with Stat Studio. To open the program containing the statements:
To create a DataObject from a SAS data set on the client, you can use the CreateFromFile method of the DataObject class. Type or copy the following statements into a program window, and select Program Run from the main menu.
declare DataObject dobj; dobj = DataObject.CreateFromFile("Hurricanes"); DataTable.Create( dobj );
The first statement declares dobj to be an IMLPlus variable that refers to a DataObject. The second statement creates the DataObject and populates it with data from the specified data set. (More information about methods in the DataObject class is available in the Stat Studio online Help.)
If you omit the file extension from the argument to CreateFromFile (as in this example), an extension of sas7bdat is assumed.
The last statement creates a DataTable, which displays the data in tabular form. The data table might appear behind your program window, so move the program window if necessary. While it is reassuring to see the data table and to know that your data set was correctly opened, this last step is not necessary: the DataObject is created even if you do not create a DataTable.
Note: You can specify an absolute Windows pathname for the argument to the CreateFromFile method. If, however, you specify a partial pathname as in the preceding example, then Stat Studio searches for the file relative to certain directories. The Hurricanes data set is distributed with Stat Studio. The directory containing Stat Studio sample data sets (C:\Program Files\SAS\Stat Studio\3.1\Data Sets) is, by default, one of the directories automatically searched. See the Stat Studio online Help for information about how to add or change directory search paths.
If your data are stored in a SAS data library, such as work, sashelp, or sasuser, or in a libref that you created using the LIBNAME statement, you can open the data by using a similar method. The method's name is CreateFromServerDataSet. It is valid to have a LIBNAME statement in an IMLPlus program, so you can define your libref in the same program in which you use the CreateFromServerDataSet method.
Add the following statements at the bottom of the program window, and select Program Run from the main menu.
declare DataObject dobjServer; dobjServer = DataObject.CreateFromServerDataSet("SASHELP.CLASS"); DataTable.Create( dobjServer );
Again, the data table might appear behind your program window, so move the program window if necessary. It is not necessary to create a DataTable unless you want to see a tabular view of the data.
If you already have a DataObject and you want to add variables from a server data set to it, then use the CopyServerDataToDataObject module as discussed in the section "Using IML Matrices to Store Data" and Chapter 5, "Adding Variables to the DataObject."
Copyright © 2008 by SAS Institute Inc., Cary, NC, USA. All rights reserved.