Previous Page | Next Page

Reading and Writing Data

Use a Program to Write a SAS Data Set

In the previous section you learned programming statements to read SAS data sets. In this section you learn programming statements to write SAS data sets.

Save Data to the Client

If you want to save data in a DataObject to a SAS data set on your PC, use the WriteToFile method.

Add the following statement at the bottom of the program window, and select Program Run from the main menu.

 
   dobj.WriteToFile("C:\MyHurricanes.sas7bdat"); 

You must append the sas7bdat file extension to the filename when you use the WriteToFile method. If you omit the extension, you get an error message that notifies you that the file format is not supported.

It is often convenient to save data to your personal files directory, as described in the section Open a Client Data Set. You can get the Windows path for your personal files directory by using the GetPersonalFilesDirectory module. The following statements save data to the Data Sets folder under your personal files directory:

 
   run GetPersonalFilesDirectory( path ); 
   fname = path + "Data Sets\MyHurricanes.sas7bdat"; /* concatenate strings */ 
   dobj.WriteToFile( fname ); 

Save Data to a SAS Library

If you want to save certain variables in a DataObject to a data set in a SAS library, use the WriteVarsToServerDataSet method of the DataObject.

Add the following statement at the bottom of the program window, and select Program Run from the main menu.

 
   dobj.WriteVarsToServerDataSet( {"Name" "latitude" "longitude"}, 
         "Work", "HurrLatLon", true ); 

The first argument of the WriteVarsToServerDataSet method specifies the name of the variables in the DataObject that you want to write to the server. The next two arguments specify the SAS library (Work) and name of the data set (HurrLatLon) to be written. The last argument is a Java boolean argument. If you specify true, then observations that are marked "Exclude from Analysis" are not written to the server. If you specify false, then all observations are written to the server.

Note:Java is a case sensitive language, so the last argument of the WriteVarsToServerDataSet method must be lowercase.

If you want to write all variables to a SAS data set on the server, you can use the WriteToServerDataSet method. It is more efficient to write only the variables that are relevant to your analysis. For example, if you intend to run a regression that relates a single response variable to two explanatory variables, you should write only those three variables to the server data set.

View Data Sets in a SAS Library

There are two ways to verify that your data were written to a SAS library as you intended. The first way is to use the PRINT or CONTENTS procedure to print information to the output window. Add the following statements at the bottom of the program window, and select Program Run from the main menu.

 
   submit; 
   proc print data=HurrLatLon(obs=10); 
   run; 
   endsubmit; 

The SUBMIT and ENDSUBMIT statements are discussed in Chapter 4, Calling SAS Procedures. Anything between these two statements is passed from IMLPlus to the SAS System for processing. The result of these statements is to print the first 10 observations of the Work.HurrLatLon data set to the output window.

The second way to view a data set in a SAS library is to open the data set in SAS/IML Studio as explained in the section Open a Server Data Set.

Previous Page | Next Page | Top of Page