Previous Page | Next Page

Statements

LOCK Statement



Acquires and releases an exclusive lock on an existing SAS file.
Valid: Anywhere
Category: Program Control
Restriction: You cannot lock a SAS file that another SAS session is currently accessing (either from an exclusive lock or because the file is open).
Restriction: 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
Details
General Information
Acquiring Exclusive Access to a SAS File in a Single-User Environment
Return Codes for the LOCK Statement
Comparisons
Examples
Example 1: Locking a SAS File
See Also

Syntax

LOCK libref<.member-name<.member-type | .entry-name.entry-type>> <LIST | QUERY | SHOW | CLEAR> ;


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. You release an exclusive lock by using the CLEAR option.


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:

For more information about the SYSLCKRC SAS macro variable, see SAS Macro Language: Reference.


Comparisons


Examples


Example 1: 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

Data Set Option:

CNTLLEV= Data Set Option

For information about locking a data object in a library that is accessed via a multiuser SAS/SHARE server, see the LOCK statement in the SAS/SHARE User's Guide.

Previous Page | Next Page | Top of Page