Reading and Writing Data

Using 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.

Saving Data to the Client

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

Add the following statement at the bottom of the program window, and select Program \blacktriangleright\,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 saying that the file format is not supported.

It is often convenient to save data to your personal files directory, as described in the "Opening Client Data Sets" section. 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 );
 

Saving 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 \blacktriangleright\,Run from the main menu.

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

The first argument to 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.

Caution: Java is a case sensitive language, so the last argument to 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.

Viewing 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 PROC PRINT or PROC CONTENTS to print information to the output window. Add the following statements at the bottom of the program window, and select Program \blacktriangleright\,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 SAS 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 Stat Studio as explained in the section "Opening Server Data Sets".

Previous Page | Next Page | Top of Page