This code is working perfectly when we pass single file but I need to achieve the same when I pass multiple files. If any file fails any macro condition it should be logged in the error.csv.
Here I do have 2 macros, whenever I pass a file it should go through the 2 macros and if encounters any issues it should write the error msg in the errorlog.csv.
Macro 1: get_FileExtension - used to check whether the file which is passed have valid extensions(TXT, TTX, SAS7BDAT, CSV). IF the passed file has no valid extension it writes log in errorlog.csv ("Invalid file extension for file &file")
Macro 2: check_file_exists - used to check whether the file which is passed is on the server. IF the passed file is not on the server it writes log in errorlog.csv ("The file &filepath does not exist on the server")
%macro get_FileExtension(file, path);
%local extension logtext;
%let extension = %upcase(%sysfunc(scan(&file.,-1,.)));
%if &extension. = TXT or &extension. = TTX %then %let extension='09'x;
%else %if &extension. = CSV %then %let extension='CSV';
%else %if &extension. = SAS7BDAT %then %let extension='SAS';
%else %do;
%let logtext = ERROR: Invalid file extension for file &file.;
%put &logtext;
%global error_message;
%let error_message = &logtext;
data null;
file errorlog mod;
/* Check if headers have been written */
if n = 1 then put "Date Time Message";
put "&sysdate9. &sysampm. &logtext";
run;
%let extension='UNKNOWN';
%end;
&extension.
%mend get_FileExtension;
%macro check_file_exists(filepath);
%local logtext;
%if %sysfunc(fileexist(&filepath.)) %then %do;
%let logtext = NOTE: The file &filepath exists on the server.;
%put &logtext;
%end;
%else %do;
%let logtext = ERROR: The file &filepath does not exist on the server.;
%put &logtext;
%global error_message;
%let error_message = &error_message. %str( ) &logtext;
data null;
/* Check if headers have been written */
if n = 1 then put "Date Time Message";
put "&sysdate9. &sysampm. &logtext";
run;
%end;
%mend check_file_exists;
%macro check_and_get_FileExtension(file, path);
%local extension;
%let extension = %get_FileExtension(&file, &path);
%check_file_exists(&file);
%put NOTE: File Extension is &extension.;
%put &error_message.;
data null;
file "//home/ketha/errorlog.csv" mod;
length message $200; /* Adjust the length as per your requirement */
message = catx(' ', "&sysdate9.", "&sysampm.", "&error_message.");
put message;
run;
%mend check_and_get_FileExtension;
%let file=/home/ketha/eoth_bshg.invalidext;
%let path=/home/ketha;
%check_and_get_FileExtension(&file., &path.);
Output for invalid file:
ERROR: Invalid file extension for file /home/ketha/eoth_bshg.invalidext
ERROR: The file /home/ketha/eoth_bshg.invalidext does not exist on the server.
I tried the same above for multiple files but it is failing. I need help to achieve the same on multiple files.
A lazy but efficient approach would be to write another macro to iterate over your list of paths/files. Of note, you'll want to take care how they are delimited (e.g. can your files/paths contain spaces?). A simple example:
There is a lot I would extend here, for example the code assumes that
filesandpathscontain an equal number ofdelim-delimited values, there's zero consideration for macro quoting, this will do nothing iffilesis blank, etc.