BookmarkSubscribeRSS Feed
AWHT
Calcite | Level 5

Hi there,

 

I have been getting this error when reading a list of log files. Some of them can be 'hot' running some house cleaning at the background.

Instead of bombing out each time, is there a way to know which one is currently being written to so that I can skip reading it altogether? Or any other approach as long as SAS EG won't error out.

 

ERROR: Resource is write-locked by another user. File=/somepath/abc_yymmdd.log. System Error Code = 11.
ERROR: File is in use, /somepath/abc_yymmdd.log.

3 REPLIES 3
Shmuel
Garnet | Level 18

As much as I remember SAS adds a suffix .lck to locked files, any how I'm not sure is it done to log files too. It may depend on OS.

Check the directory where logs are kept and see if there are files with suffix .log.lck

or .loglck or alike.

 

Alternatively, if you need read only the log (not writing to it) maybe you can open it

with the READONLY option.

 

Kurt_Bremser
Super User

Log files do not have an additional .lck extension when being written to, and, at least on UNIX, SAS does not have the file locked while it is used, as you can always read it while the program is still running.

s_lassen
Meteorite | Level 14

I assume that you are reading a number of files in a single data step, and that the whole thing crashes when one of the files are not available.

 

I think the way to go is to use the FOPEN or MOPEN function. That should just return a zero if the file is not available for reading, in which case you can use the SYSRC() and SYSMSG() functions to see what went wrong.

 

For instance, if you are reading all the .log files from a specific directory, it can be done something like this:

filename logdir 'c:\saslogs';

data loglines;
  dir_id=dopen('logdir');
  if dir_id=0 then do;
    msg=sysmsg();
    put msg=;
    stop;
    end;
  do _N_=1 to dnum(dir_id);
    filename=dread(dir_id,_N_);
    if upcase(scan(filename,-1,'.'))='LOG' then do;
      file_id=mopen(dir_id,_N_);
      if file_id=0 then do;
        msg=sysmsg();
        put filename= msg=;
        end;
      else do;
         /* code to read the file, one option is to use FREAD and FGET: */
         do line_no=1 by 1 while(fread(file_id)=0);
           length line $200;
           rc=fget(file_id,line);
           output;
           end;
         rc=fclose(file_id);
         end;
      end;
    end;
  rc=dclose(dir_id);
  drop dir_id file_id rc;
  stop;
run;

Of course, you may want to replace the OUTPUT statement with some more advanced processing of the log line, such as looking for errors or warnings, and only outputting those.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 1327 views
  • 4 likes
  • 4 in conversation