SAS locks

Posted on 2011-09-26
Last Modified: 2013-11-16
SAS 9.2 ts2m3.
Windows XP SP3
AIX 6.1

I am trying to understand the locking mechanism in SAS. We have a problem with our batch updates failing if a user is querying a dataset while we are trying to reload it. I would like to identify the lock, send an email and loop instead of having my process fail.

I tried this test and get unexpected results.
1) Start PCSAS
2) Connect to my SAS server on an AIX box.
3) Create a libname to the server.
4) Lock my dataset successfully. Lock libname.dataset;  From the log: "NOTE: MLQ_WKL.DAILYSALES.DATA is now locked for exclusive access by you."

5) Create a second PCSAS instance
6) Connect to the same AIX box.
7) Create the same libname as above.
8) Test the lock. Lock libname.dataset list; This returns the message "NOTE: MLQ_WKL.DAILYSALES.DATA is not locked or in use by you or any other users.

If I try to open the dataset in SAS Explorer I get the message below.

Why do I get the message in #8? And can I query SAS successfully for a lock to prevent my process from failing?

T-Bone SAS message example
Question by:sanofi-aventis
  • 3
  • 2
  • 2
  • +1

Accepted Solution

d507201 earned 200 total points
ID: 36599429
I've never seen the lock function, and it's not in the 9.1 onl-line documentation's list of functions.  So I can't comment on what's going on there.  

I do know that unlike a true RDBMS, SAS applies file-level locking during update activity.  Go to the onl-line documentation and search for 'locking'... lots of useful hits.  To allow reading while writing, you need SAS/SHARE.  It lets SAS to apply record-level locking during updates.

To test if a dataset is locked, use the OPEN and CLOSE functions.  I haven't tested it, but I'm pretty sure that the OPEN function will fail if a dataset is locked.  So try to open the dataset and if the return code is zero then the open failed and you can send an email. and then abort the job  Non-zero means it worked--then CLOSE it and continue processing.

Something like this--

%MACRO TEST_IF_LOCKED(_datasetName);
%let dsid=%sysfunc(open(&_datasetName,i));
%if (&dsid = 0) %then %do;
  ** send email. ;
%else %do;
LVL 11

Assisted Solution

theartfuldazzler earned 200 total points
ID: 36599469

I would look to use the SYSLCKRC macro variable...  but the best is to look at the following link:



Expert Comment

ID: 36599715
Very nice solution.  I'm always pleasantly surprised at the willingness of the folks at d-Wise to share their tips and clues.

I'd say that testing availability after opening and closing the dataset is maybe a bit much.  But if there's a chance of a timing error, I'd just put a 1-second SLEEP function immediately after the CLOSE function and not deal with locking and unlocking the dataset.  I use the sleep function quite often when I'm concerned about timing.

"Since the DATA step did some preliminary checking, it is likely that the lock statement will now succeed. However, due to split-second timing it is still possible that the lock statement will fail."

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!


Author Comment

ID: 36600128
Thanks guys. I've google this solution myself. I understand the concept of trying a file level open prior to using the file. I do not consider this querying SAS for a lock status. The Lock statement seems to be OK. Even though I have to query my log file to get the result, it does give me the process id that has it locked. I'm surprised the Proc Dataset, or one of the other procedures or even the Dictionary views, does not have an option to return the lock status.

At this time, I don't even plan to lock the dataset prior to using it, I just want to know if someone has it locked prior to truncating it and reloading.

and still not sure why the second PCSAS session returns the message "NOTE: MLQ_WKL.DAILYSALES.DATA is not locked or in use by you or any other users.

when I cannot open the dataset in that session's SAS Explorer. I get the message in the picture above.
LVL 14

Assisted Solution

by:Aloysius Low
Aloysius Low earned 100 total points
ID: 36647187
if it's PCSAS you are talking about (vs connecting to the same SAS instance on the server), a possible explanation is because it's by a different SAS instance, hence the lock status is not recognised? but when you try to open the dataset, it tries to get the dataset locked but only to find out that it was actually already locked...

i stand corrected here - i'm just speculating, making sense of what has happened...
LVL 11

Expert Comment

ID: 36707699
Hi T-Bone

I must admit - I don't know AIX.

I have done this on a Windows server using commands such as handle.exe, processExplorer and pslist.  In theory there are similar commands in Unix - but you would need to post this question (or a related one) on the IBM AIX UNIX Zone.

Despite my lack of AIX knowlegde, I did some more googling, and came across this link:

In theory, one would just need to find the PID file (I would assume it sits in the same directory as the SAS dataset), read in the PID file as text, and get the process id number that way?

I have no way to test this - if you gon't come right - try this question on the AIX Zone.


Author Comment

ID: 36709376

Thank you for your comments. But neither get me any further than assumptions already attained.

You would think that if the engine allows locks and can give a message like in the picture above that this information would be available to the programmer/analyst other than just an error code when trying to access the resource.

I will keep this question open for now.


Author Closing Comment

ID: 36815937
Thanks all.
You can lock files in SAS.
You cannot passively identify if there is a lock on a dataset.
Only by trying to lock it and querying the SYSLCKRC macro variable will you know if you can have a lock or not.
I will need to use the operating system to know who has it locked. For Unix I am going to try the fuser & ps command to get the users id. fuser for the pid then ps for the user id that has it closed.
I still need to figure out how to get the email name from the users id.

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes some very basic things about SQL Server filegroups.
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question