As the first step in the decommissioning of sasCommunity.org the site has been converted to read-only mode.
Here are some tips for How to share your SAS knowledge with your professional network.
- This article is a work in progress. Please contribute what you know about the subject.
The LOCK statement allows a SAS program apply an exclusive lock to a data set. The lock will prevent other programs from either reading from or writing to that data set until the lock is released or the program that applied the lock terminates. This can be handy in cases where multiple programmers have access to SAS data sets residing on shared disk space; such as can be found on network drives, Windows servers, UNIX servers, etc. Using the LOCK statement can guarantee that a program has exclusive control of a data set for the period that it is needed.
- 1 Syntax
- 2 Usage
- 3 Further reading
- LOCK data-set-name <option> ;
- data-set-name is the name of the data set including its library. e.g. templib.orsales You can also specify which particular member of the data set you want to lock. By default the .DATA member is locked, although you can instead lock the .VIEW or .CATALOG member. If only the library name is given, all data sets in a library can be locked.
- <option> is an optional key word.
- If there is no <option> keyword then an attempt is made to lock the data set.
- If the <option> keyword is CLEAR then an attempt is made to release the lock on the data set.
- If the <option> keyword is one of LIST, SHOW or QUERY then the status of any lock on the data set is displayed in the log.
- In SAS 9.4, if the <option> keyword is NOMSG then no messages are written in the log. This may be helpful if you are testing for a successful lock within a macro loop as it will prevent repeated error message appearing in the log.
The LOCK statement is a global Base SAS statement. It is executed immediately when it is encountered in open code. It can also be used inside a macro.
The LOCK statement can only lock a data set once. If you have already locked the data set, trying to lock the data set a second time produces an error message in the log.
LOCK statement error messages
Although a LOCK statement may produce a error message, the SAS system might not indicate that an error has occurred. While the automatic macro variable &SYSERR may return the value of 0, the automatic macro variable &SYSERRORTEXT will still contain the text of the error message produced by the LOCK statement.
LOCK statement return code
When the LOCK statement executes, a return code is put into the automatic macro variable &SYSLCKRC. The value contained in &SYSLCKRC indicates if the LOCK statement was successful or not.
- A value of 0 (zero) indicates the LOCK statement was successful in obtaining (or clearing) a lock.
- A value larger than zero indicates the LOCK statement was unable to obtain (or clear) a lock.
- A value smaller than zero indicates the LOCK statement was completed without an error. This could mean the data set had already been locked previously, or the lock status was only listed, shown or queried by the statement.
- The particular value returned depends on the operating system and any error state encountered.
Waiting for a lock when a data set is already locked by another user
The LOCK statement will respect the FILELOCKWAIT library option, if this is set, and will wait the designated timeout period before returning an error if a data set is currently being used by another user.
Unlike DATA steps and Procedures, the LOCK statement does not produce any performance statistics for when it starts and finishes. If you want performance statistics you need to capture the start and finish times using macro code.
Testing that a data set can be locked
The LOCK statement LIST, SHOW, or QUERY option will not detect that a data set is being read by another user. If you want to check that a data set is available for locking and not being read by another user then you need to attempt to modify the data set. For more information see Sample 51275: Locking a data set and verifying that a lock is held.
Alternatives to the LOCK statement
The LOCK statement is a fairly blunt instrument since it takes exclusive control of the whole data set. This might not be appropriate in all database sharing environments. While, LOCK can be used to simulate some of the capabilities of SAS/SHARE, you may find that there is improved functionality with using SAS/SHARE instead.
Operating system file locks
Even though you lock a dataset, this is not the same as an operating system level lock on all the files relating to the dataset. The associated index file is not locked until you attempt to modify the index in some way. The index file can still be read by another operating system process (e.g. File copy), even though other SAS users cannot access the dataset. If the index file is being read when an attempt is made to update it or the dataset, that will cause an error indicating the dataset has been damaged. Once the other operating system process has finished reading the file, the dataset will be found to be undamaged.