SASFILE Statement

Opens a SAS data set and allocates enough buffers to hold the entire file in memory.
Valid in: Anywhere
Category: Program Control
Restriction: A SAS data set opened by the SASFILE statement can be used for subsequent input (read) or update processing but not for output or utility processing.
See: SASFILE Statement: z/OS in SAS Companion for z/OS in SAS Companion for z/OS

Syntax

Arguments

libref
a name that is associated with a SAS library. The libref (library reference) must be a valid SAS name. The default libref is either USER (if assigned) or WORK (if USER not assigned).
Restriction:The libref cannot represent a concatenation of SAS libraries that contain a library in sequential format.
member-name
a valid SAS name that is a SAS data file (a SAS data set with the member type DATA) that is a member of the SAS library associated with the libref.
Restriction:The SAS data set must have been created with the V7, V8, or V9 Base SAS engine.
member-type
the type of SAS file to be opened. Valid value is DATA, which is the default.
password-option(s)
specifies one or more of the following password options:
READ=password
enables the SASFILE statement to open a read-protected file. The password must be a valid SAS name.
WRITE=password
enables the SASFILE statement to use the WRITE password to open a file that is both read-protected and write-protected. The password must be a valid SAS name.
ALTER=password
enables the SASFILE statement to use the ALTER password to open a file that is both read-protected and alter-protected. The password must be a valid SAS name.
PW=password
enables the SASFILE statement to use the password to open a file that is assigned for all levels of protection. The password must be a valid SAS name.
Tip:When SASFILE is executed, SAS checks whether the file is read-protected. Therefore, if the file is read-protected, you must include the READ= password in the SASFILE statement. If the file is either write-protected or alter-protected, you can use a WRITE=, ALTER=, or PW= password. However, the file is opened only in input (read) mode. For subsequent processing, you must specify the necessary password or passwords. See Specifying Passwords with the SASFILE Statement.
OPEN
opens the file, allocates the buffers, but defers reading the data into memory until a procedure, statement, or application is executed.
LOAD
opens the file, allocates the buffers, and reads the data into memory.
Note: If the total number of allowed buffers is less than the number of buffers required for the file based on the number of data set pages and index file pages, SAS issues a warning to tell you how many pages are read into memory.
CLOSE
frees the buffers and closes the file.

Details

General Information

The SASFILE statement opens a SAS data set and allocates enough buffers to hold the entire file in memory. Once it is read, data is held in memory, available to subsequent DATA and PROC steps or applications, until either a second SASFILE statement closes the file and frees the buffers or the program ends, which automatically closes the file and frees the buffers.
Using the SASFILE statement can improve performance by
  • reducing multiple open or close operations (including allocation and freeing of memory for buffers) to process a SAS data set to one open or close operation
  • reducing I/O processing by holding the data in memory.
If your SAS program consists of steps that read a SAS data set multiple times and you have an adequate amount of memory so that the entire file can be held in real memory, the program should benefit from using the SASFILE statement. Also, SASFILE is especially useful as part of a program that starts a SAS server such as a SAS/SHARE server. However, as with most performance-improvement features, it is suggested that you set up a test in your environment to measure performance with and without the SASFILE statement.

Processing a SAS Data Set Opened with SASFILE

When the SASFILE statement executes, SAS opens the specified file. Then when subsequent DATA and PROC steps execute, SAS does not have to open the file for each request; the file remains open until a second SASFILE statement closes it or the program or session ends.
When a SAS data set is opened by the SASFILE statement, the file is opened for input processing and can be used for subsequent input or update processing. However, the file cannot be used for subsequent utility or output processing, because utility and output processing requires exclusive access to the file (member-level locking). For example, you cannot replace the file or rename its variables.
The following table provides a list of some SAS procedures and statements and specifies whether they are allowed if the file is opened by the SASFILE statement:
Processing Requests for a File Opened by SASFILE
Processing Request
Open Mode
Allowed
APPEND procedure
update
Yes
DATA step that creates or replaces the file
output
No
DATASETS procedure to rename or add a variable, add or change a label, or add or remove integrity constraints or indexes
utility
No
DATASETS procedure with AGE, CHANGE, or DELETE statements
does not open the file but requires exclusive access
No
FSEDIT procedure
update
Yes
PRINT procedure
input
Yes
SORT procedure that replaces original data set with sorted one
output
No
SQL procedure to modify, add, or delete observations
update
Yes
SQL procedure with CREATE TABLE or CREATE VIEW statement
output
No
SQL procedure to create or remove integrity constraints or indexes
utility
No

Buffer Allocation

A buffer is a reserved area of memory that holds a segment of data while it is processed. The number of allocated buffers determines how much data can be held in memory at one time.
The number of buffers is not a permanent attribute of a SAS file. That is, it is valid only for the current SAS session or job. When a SAS file is opened, a default number of buffers for processing the file is set. The default depends on the operating environment but typically is a small number such as one buffer. To specify a different number of buffers, you can use the BUFNO= data set option or system option.
When the SASFILE statement is executed, SAS automatically allocates the number of buffers based on the number of data set pages and index file pages (if an index file exists). For example:
  • If the number of data set pages is five and there is not an index file, SAS allocates five buffers.
  • If the number of data set pages is 500 and the number of index file pages is 200, SAS allocates 700 buffers.
If a file that is held in memory increases in size during processing, the number of allocated buffers increases to accommodate the file. Note that if SASFILE is executed for a SAS data set, the BUFNO= option is ignored.

I/O Processing

An I/O (input/output) request reads a segment of data from a storage device (such as disk) and transfers the data to memory, or conversely transfers the data from memory and writes it to the storage device. When a SAS data set is opened by the SASFILE statement, data is read once and held in memory, which should reduce the number of I/O requests.
CAUTION:
I/O processing can be reduced only if there is sufficient real memory.
If the SAS data set is very large, you might not have sufficient real memory to hold the entire file. If insufficient memory exists, your operating environment can simulate more memory than actually exists, which is virtual memory. If virtual memory occurs, data access I/O requests are replaced with swapping I/O requests, which could result in no performance improvement. In addition, both SAS and your operating environment have a maximum amount of memory that can be allocated, which could be exceeded by the needs of your program. If your program needs exceed the memory that is available, the number of allocated buffers might be decreased to the default allocation in order to free memory.
Tip
To determine how much memory a SAS data set requires, execute the CONTENTS procedure for the file to list its page size, the number of data set pages, the index file size, and the number of index file pages.

Using the SASFILE Statement in a SAS/SHARE Environment

The following are considerations for using the SASFILE statement with SAS/SHARE software:
  • You must execute the SASFILE statement before you execute the PROC SERVER statement.
  • If the client (the computer on which you use a SAS session to access a SAS/SHARE server) executes the SASFILE statement, it is rejected.
  • Once the SASFILE statement is executed, all users who subsequently open the file will access the data held in memory instead of data that is stored on the disk.
  • Once the SASFILE statement is executed, you cannot close the file and free the buffers until the SAS/SHARE server is terminated.
  • You can use the ALLOCATE SASFILE command for the PROC SERVER statement as an alternative that brings part of the file into memory (controlled by the BUFNO= option).
  • If the SASFILE statement is executed and you execute ALLOCATE SASFILE specifying a value for BUFNO= that is a larger number of buffers than allocated by SASFILE, performance will not be improved.

Comparisons

  • Use the BUFNO= system option or data set option to specify a specific number of buffers.
  • With SAS/SHARE software, you can use the ALLOCATE SASFILE command for the PROC SERVER statement to bring part of the file into memory (controlled by the BUFNO= option).

Examples

Example 1: Using SASFILE in a Program with Multiple Steps

The following SAS program illustrates the process of opening a SAS data set, transferring its data to memory, and reading that data held in memory for multiple tasks. The program is consists of steps that read the file multiple times.
libname mydata 'SAS-library';
sasfile mydata.census.data open;1 
data test1; 
   set mydata.census;2 
run;
data test2; 
   set mydata.census;3 
run;
proc summary data=mydata.census print;4 
run;
data mydata.census;5 
   modify mydata.census; 
   .
   . (statements to modify data)
   .
run;
sasfile mydata.census close;6 
1 Opens SAS data set MYDATA.CENSUS, and allocates the number of buffers based on the number of data set pages and index file pages.
2 Reads all pages of MYDATA.CENSUS, and transfers all data from disk to memory.
3 Reads MYDATA.CENSUS a second time, but this time from memory without additional I/O requests.
4 Reads MYDATA.CENSUS a third time, again from memory without additional I/O requests.
5 Reads MYDATA.CENSUS a fourth time, again from memory without additional I/O requests. If the MODIFY statement successfully changes data in memory, the changed data is transferred from memory to disk at the end of the DATA step.
6 Closes MYDATA.CENSUS, and frees allocated buffers.

Example 2: Specifying Passwords with the SASFILE Statement

The following SAS program illustrates using the SASFILE statement and specifying passwords for a SAS data set that is both read-protected and alter-protected:
libname mydata 'SAS-data-data-library';
sasfile mydata.census (read=gizmo) open;1  
proc print data=mydata.census (read=gizmo);2  
run;
data mydata.census;
   modify mydata.census (alter=luke);3 
   .
   . (statements to modify data)
   .
run;
1 The SASFILE statement specifies the READ password, which is sufficient to open the file.
2 In the PRINT procedure, the READ password must be specified again.
3 The ALTER password is used in the MODIFY statement, because the data set is being updated.
Note: It is acceptable to use the higher-level ALTER password instead of the READ password in the above example.

See Also

For information about using the SASFILE statement in a SAS/SHARE environment, see The SERVER Procedure in SAS/SHARE User's Guide.
Data Set Options:
BUFNO= Data Set Option in SAS Data Set Options: Reference
System Options:
BUFNO= System Option in SAS System Options: Reference