DATA Step, Macro, Functions and more

Acquire a lock on SAS data set

Posts: 21

Acquire a lock on SAS data set


I need change a SAS data set's name while someone is reading the data from the data set.

It does not matter the renaming interrupts the reading.

How can I acquire the a lock and force to rename the data set?

Thanks in advance!


Super User
Posts: 3,114

Re: Acquire a lock on SAS data set

I don't believe this is possible - not without SAS/Share anyway. SAS uses operating system locking. If a SAS dataset is locked while being read, it means the file is locked by the operating system so you can't "force" a lock and rename it.

What operating system are you using? I believe Unix may be more flexible in this area than Windows. If you were using SAS/Share and the dataset was being accessed via a SAS/Share server then I think this might be possible.  

Posts: 21

Re: Acquire a lock on SAS data set

Thanks for the information.

It is on Unix box.

It might not be a OS level lock.

I tested with a small text file:  Open a file with vi in one session, and I still can rename it with mv in another session.

However, even for small SAS data set, when one session reading the data set, it is not allowed to change the name with proc datasets.

It can be renamed with mv using xcmd, but it is not a "pure" SAS way...

Trusted Advisor
Posts: 1,300

Re: Acquire a lock on SAS data set

To obtain a lock you can use the lock statement.

data foo;

set sashelp.class;



proc datasets lib=work;

change foo=bar;


lock clear;

I remember an old paper that contained a macro call %trylock, which you may find useful.  You cannot lock a SAS file or modify one by force when a lock is held by another process in SAS.

You could try having the session performing the read use cntllev=rec, but I have not tested it.

I found the paper, I think:

Valued Guide
Posts: 2,175

Re: Acquire a lock on SAS data set

the problem is that you cannot stop the current user/use of a table to apply the lock.

What would be nice is a RESERVE type of lock.

Macro %TRYLOCK() is an attempt to obtain a lock. However this is os-dependent - if it doesn't support that functionality. I think there is nothing else to hold that RESERVE-request.

Respected Advisor
Posts: 3,900

Re: Acquire a lock on SAS data set

I've tried in the past to make this %trylock() macro work failsafe but with no real success. So can't really recommend to use it.

Ask a Question
Discussion stats
  • 5 replies
  • 5 in conversation