Previous Page | Next Page

Reading and Writing Data

Use a Program to Read a SAS Data Set

As explained in Chapter 1, Introduction to SAS/IML Studio, 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 NewWorkspace from the main menu.

The program statements in this chapter are distributed with SAS/IML Studio. To open the program that contains the statements:

Select File Open File from the main menu.

  • Click Go to Installation directory near the bottom of the dialog box.

  • Navigate to the Programs\Doc\STATGuide folder.

  • Select the file.

  • Click Open.

  • Open a Client Data Set

    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 SAS/IML Studio online Help.)

    If you omit the file extension from the argument of 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 statement 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 passed to the CreateFromFile method. However, if you specify a partial pathname as in the preceding example, then SAS/IML Studio searches for the file relative to certain directories. The Hurricanes data set is distributed with SAS/IML Studio. The directory that contains Stat Studio sample data sets (C:\Program Files\SAS\SASIMLStudio\3.2\Data Sets) is, by default, one of the directories automatically searched. See the SAS/IML Studio online Help for information about how to add or change directory search paths.

    Open a Server Data Set

    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 Use SAS/IML Matrices to Store Data and Chapter 5, Adding Variables to the DataObject.

    Previous Page | Next Page | Top of Page