Five Reasons to CLOSE Your Data Sets


My mother taught me to put things away when I'm finished using them. She doesn't use a computer, but if she did, I know that she'd approve of this tip from my book:

Tip: Always close your files and data sets when you are finished reading or writing them.

In my blog, book, and presentations, I always read data from a SAS data set into SAS/IML vectors by using statements such as the following:

proc iml;
/** read variables into vectors of the same name **/
use Sashelp.Class;           /** open data set for reading **/
read all var {weight height};/** read data **/
close Sashelp.Class;         /** close data set **/

Many SAS/IML programmers omit the CLOSE statement. After all, they reason, PROC IML closes all open data sets when it encounters a QUIT statement, so why use the CLOSE statement?

I can think of five reasons why closing your data set is a good programming practice:

  1. It frees up memory.
  2. It frees up system resources.
  3. It prevents corruption of the data if you terminate the program while it is running.
  4. It clearly indicates that you are finished using the data set.
  5. It keeps notes in the SAS log in synch with the program statements. (See the next section for details.)

Can you think of other reasons? If so, post a comment.

Notes That Are Out of Synch?

The last reason on the list requires further explanation. Consider the following statements:

x = {1, 2, 3, 4, 5};
create Temp var {x};
/** finished creating data set, but no CLOSE statement **/
/** program continues for many more lines **/
call delete(Temp); /** delete temporary data sets **/

What does the SAS log look like?

13   call delete(Temp);
NOTE: The data set WORK.TEMP has 5 observations and 1 variables.

Well, isn't that interesting? When PROC IML encounters the DELETE call, it recognizes that the Temp data set is still open. It needs to close the data set before it can delete it. When the data set is closed, the note appears in the log. The note informs you that the Temp data set has been successfully created, but it appears in the log after the DELETE call!

The program works just fine (the data set is successfully deleted), but the note is confusing when it appears after the DELETE call. If you use the CLOSE statement after the APPEND statement, the note appears earlier in the log and there is no potential for confusion.

Thanks for the advice, Mom!


About Author

Rick Wicklin

Distinguished Researcher in Computational Statistics

Rick Wicklin, PhD, is a distinguished researcher in computational statistics at SAS and is a principal developer of SAS/IML software. His areas of expertise include computational statistics, simulation, statistical graphics, and modern methods in statistical data analysis. Rick is author of the books Statistical Programming with SAS/IML Software and Simulating Data with SAS.


  1. Pingback: Reading SAS Data Sets - The DO Loop

  2. Pingback: Readers’ choice 2010: The DO Loop’s most-read posts - The DO Loop

  3. Pingback: The macro loop that vanished: How to simplify your SAS/IML programs - The DO Loop

  4. Pingback: Read from one data set and write to another with SAS/IML - The DO Loop

Leave A Reply

Back to Top