Go Premium for a chance to win a PS4. Enter to Win


SAS locks

Posted on 2011-09-26
Medium Priority
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 800 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 800 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."

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.


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 400 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 www.sysinternals.com 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
How much do you know about the future of data centers? If you're like 50% of organizations, then it's probably not enough. Read on to get up to speed on this emerging field.
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Suggested Courses

876 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