DATA Step, Macro, Functions and more

Skip Files In Use?

Accepted Solution Solved
Reply
Contributor
Posts: 24
Accepted Solution

Skip Files In Use?

Hello All,

 

We currently have a log scan program deployed that goes to the folder where our job logs are created and pulls in the text from each log. We then search for error, warning, etc ... 

 

The problem we are running into is how to pull the most current log files without coming across ones that are currently being created / open. When this occurs it kills the load ---

 

Is there anyway to skip files that are in use and have the program continue running?

 

Thanks,

Andrew


Accepted Solutions
Solution
‎02-10-2017 10:04 AM
Contributor
Posts: 30

Re: Skip Files In Use?

If your environment is Windows, this old macro will work. I guess it works in Unix/Linux also, because the sas file functions are general, but i cannot try it. 

 

/********************************************************************************/

/* CheckExtFile                                    Erik Lund-jensen 21 jan 2011 */

/* Macro to check if an external file can be accessed                           */

/* Use in macro:     %if %CheckExtFile(file) then ...;                          */

/* Use in Data Step: if %CheckExtFile(file) then ...;                           */

/* Return value: -1 = file exists, but is locked by another process             */

/*                0 = file does not exist                                       */

/*                1 = fil can be opened for read access                         */

/* parm to macro is file name (full path).                                      */

/********************************************************************************/

%macro CheckExtFile(inputfil);

      %local fil_findes rc filid;

      %* Check if file exists;

      %let fil_findes = %sysfunc(fileexist(&inputfil));

      %if &fil_findes = 1 %then %do;

            %* Check if it can be opened;

            %let rc=%sysfunc(filename(filref,&inputfil));

            %let filid=%sysfunc(fopen(&filref));

            %if &filid > 0 %then %do;

                  %let filcheck = 1;

                  %* Close again, if it could be opened;

                  %let rc=%sysfunc(fclose(&filid));

            %end;

            %else %let filcheck = -1;

      %end;

      %else %let filcheck = 0;

      %* No semikolon after following statement!;

      %eval(&fil_findes)

%mend;

View solution in original post


All Replies
Super User
Posts: 10,500

Re: Skip Files In Use?

You should specify your operating enviorment and configuration for this type of question as some suggestions are likely to be Operating system dependent.

 

Solution
‎02-10-2017 10:04 AM
Contributor
Posts: 30

Re: Skip Files In Use?

If your environment is Windows, this old macro will work. I guess it works in Unix/Linux also, because the sas file functions are general, but i cannot try it. 

 

/********************************************************************************/

/* CheckExtFile                                    Erik Lund-jensen 21 jan 2011 */

/* Macro to check if an external file can be accessed                           */

/* Use in macro:     %if %CheckExtFile(file) then ...;                          */

/* Use in Data Step: if %CheckExtFile(file) then ...;                           */

/* Return value: -1 = file exists, but is locked by another process             */

/*                0 = file does not exist                                       */

/*                1 = fil can be opened for read access                         */

/* parm to macro is file name (full path).                                      */

/********************************************************************************/

%macro CheckExtFile(inputfil);

      %local fil_findes rc filid;

      %* Check if file exists;

      %let fil_findes = %sysfunc(fileexist(&inputfil));

      %if &fil_findes = 1 %then %do;

            %* Check if it can be opened;

            %let rc=%sysfunc(filename(filref,&inputfil));

            %let filid=%sysfunc(fopen(&filref));

            %if &filid > 0 %then %do;

                  %let filcheck = 1;

                  %* Close again, if it could be opened;

                  %let rc=%sysfunc(fclose(&filid));

            %end;

            %else %let filcheck = -1;

      %end;

      %else %let filcheck = 0;

      %* No semikolon after following statement!;

      %eval(&fil_findes)

%mend;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 121 views
  • 1 like
  • 3 in conversation