LOCK Statement

Acquires and releases an exclusive lock on an existing SAS file.
Valid in: Anywhere
Category: Program Control
Restrictions: You cannot lock a SAS file that another SAS session is currently accessing (either from an exclusive lock or because the file is open).

The LOCK statement syntax is the same whether you issue the statement in a single-user environment or in a client/server environment. However, some LOCK statement functionality applies only to a client/server environment.

Syntax

Arguments

libref
is a name that is associated with a SAS library. The libref (library reference) must be a valid SAS name. If the libref is SASUSER or WORK, you must specify it.
Tip:In a single-user environment, you typically would not issue the LOCK statement to exclusively lock a library. To lock a library that is accessed via a multiuser SAS/SHARE server, see the LOCK statement in the SAS/SHARE User's Guide.
member-name
is a valid SAS name that specifies a member of the SAS library that is associated with the libref.
Restriction:The SAS file must be created before you can request a lock. For information about locking a member of a SAS library when the member does not exist, see the SAS/SHARE User's Guide.
member-type
is the type of SAS file to be locked. For example, valid values are DATA, VIEW, CATALOG, MDDB, and so on. The default is DATA.
entry-name
is the name of the catalog entry to be locked.
Tip:In a single-user environment, if you issue the LOCK statement to lock an individual catalog entry, the entire catalog is locked; you typically would not issue the LOCK statement to exclusively lock a catalog entry. To lock a catalog entry in a library that is accessed via a multiuser SAS/SHARE server, see the LOCK statement in the SAS/SHARE User's Guide.
entry-type
is the type of the catalog entry to be locked.
Tip:In a single-user environment, if you issue the LOCK statement to lock an individual catalog entry, the entire catalog is locked; you typically would not issue the LOCK statement to exclusively lock a catalog entry. To lock a catalog entry in a library that is accessed via a multiuser SAS/SHARE server, see the LOCK statement in the SAS/SHARE User's Guide.
LIST | QUERY | SHOW
writes to the SAS log whether you have an exclusive lock on the specified SAS file.
Tip:This option provides more information in a client/server environment. To use this option in a client/server environment, see the LOCK statement in the SAS/SHARE User's Guide.
CLEAR
releases a lock on the specified SAS file that was acquired by using the LOCK statement in your SAS session.

Details

General Information

The LOCK statement enables you to acquire and release an exclusive lock on an existing SAS file. Once an exclusive lock is obtained, no other SAS session can read or write to the file until the lock is released.
To release an exclusive lock, use the CLEAR option. In addition, an exclusive lock on a data set is released when you use the DATASETS procedure DELETE statement to delete the data set.

Acquiring Exclusive Access to a SAS File in a Single-User Environment

Each time you issue a SAS statement or a procedure to process a SAS file, the file is opened for input, update, or output processing. At the end of the step, the file is closed. In a program with multiple tasks, a file could be opened and closed multiple times. Because multiple SAS sessions in a single-user environment can access the same SAS file, issuing the LOCK statement to acquire an exclusive lock on the file protects data while it is being updated in a multistep program.
For example, consider a nightly update process that consists of a DATA step to remove observations that are no longer useful, a SORT procedure to sort the file, and a DATASETS procedure to rebuild the file's indexes. If another SAS session accesses the file between any of the steps, the SORT and DATASETS procedures would fail, because they require member-level locking (exclusive) access to the file.
Including the LOCK statement before the DATA step provides the needed protection by acquiring exclusive access to the file. If the LOCK statement is successful, a SAS session that attempts to access the file between steps will be denied access, and the nightly update process runs uninterrupted. See Locking a SAS File.

Return Codes for the LOCK Statement

The SAS macro variable SYSLCKRC contains the return code from the LOCK statement. The following actions result in a nonzero value in SYSLCKRC:
  • You try to lock a file but cannot obtain the lock (for example, the file was in use or is locked by another SAS session).
  • You use a LOCK statement with the LIST option to list a lock.
  • You use a LOCK statement with the CLEAR option to release a lock that you do not have.
For more information about the SYSLCKRC SAS macro variable, see SAS Macro Language: Reference.

Comparisons

  • With SAS/SHARE software, you can also use the LOCK statement. Some LOCK statement functionality applies only to a client/server environment.
  • The CNTLLEV= data set option specifies the level at which shared Update access to a SAS data set is denied.

Example: Locking a SAS File

The following SAS program illustrates the process of locking a SAS data set. Including the LOCK statement provides protection for the multistep program by acquiring exclusive access to the file. Any SAS session that attempts to access the file between steps will be denied access, which ensures that the program runs uninterrupted.
libname mydata 'SAS-library';
lock mydata.census; 1
data mydata.census; 2
   modify mydata.census;
   (statements to remove obsolete observations)
run;
proc sort force data=mydata.census; 3
   by CrimeRate; 
run;
proc datasets library=mydata; 4
   modify census;
   index create CrimeRate;
quit;
lock mydata.census clear;  5
1 Acquires exclusive access to the SAS data set MYDATA.CENSUS.
2 Opens MYDATA.CENSUS to remove observations that are no longer useful. At the end of the DATA step, the file is closed. However, because of the exclusive lock, any other SAS session that attempts to access the file is denied access.
3 Opens MYDATA.CENSUS to sort the file. At the end of the procedure, the file is closed but not available to another SAS session.
4 Opens MYDATA.CENSUS to rebuild the file's index. At the end of the procedure, the file is closed but still not available to another SAS session.
5 Releases the exclusive lock on MYDATA.CENSUS. The data set is now available to other SAS sessions.

See Also

For information about locking a data object in a library that is accessed via a multiuser SAS/SHARE server, see the LOCK Statement in SAS/SHARE User's Guide.
Data Set Options:
CNTLLEV= Data Set Option in SAS Data Set Options: Reference