Statements |
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 |
LOCK libref<.member-name<.member-type | .entry-name.entry-type>> <LIST | QUERY | SHOW | CLEAR> ; |
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.
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. |
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.
is the name of the catalog entry to be locked.
is the type of the catalog entry to be locked.
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. |
releases a lock on the specified SAS file that was acquired by using the LOCK statement in your SAS session.
Details |
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.
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.
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.
Examples |
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
Acquires exclusive access to the SAS data set MYDATA.CENSUS.
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.
Opens MYDATA.CENSUS to sort the file. At the end of the procedure, the file is closed but not available to another SAS session.
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.
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 the SAS/SHARE User's Guide. |
Copyright © 2011 by SAS Institute Inc., Cary, NC, USA. All rights reserved.