Hello everyone, I'm new to programming in SAS and I would like to add a check if there are 4 files in a given path.
Files with the same name but different dates will be stored in this folder, for example: 'test1_20210228.csv', the date is stored in a macro variable called &period.
What I did before was this:
%let myfilerf1=/opt/sas/data/test1_&period..csv; %let myfilerf2=/opt/sas/data/test2_&period..csv; %let myfilerf3=/opt/sas/data/test3_&period..csv; %let myfilerf4=/opt/sas/data/test4_&period..csv; %macro PROD1; %if %sysfunc(fileexist(&myfilerf1)) %then %do; %put OK; %end; %else %do; %abort cancel; %end; %mend PROD1; %PROD1 %macro PROD2; %if %sysfunc(fileexist(&myfilerf2)) %then %do; %put OK; %end; %else %do; %abort cancel; %end; %mend PROD2; %macro PROD3; %if %sysfunc(fileexist(&myfilerf3)) %then %do; %put OK; %end; %else %do; %abort cancel; %end; %mend PROD3; %macro PROD4; %if %sysfunc(fileexist(&myfilerf4)) %then %do; %put OK; %end; %else %do; %abort cancel; %end; %mend PROD4; options mprint; %PROD1 %PROD2 %PROD3 %PROD4
But the problem is that we want to automate this and the abort cancel does not work in batch, so I would like to make a kind of macro or something that sends me an email (I know how to do this) if any of those 4 files do not exist.
I had thought of concatenating the different macros of myfilerf but I don't know how to do it, to make it check if all 4 exist, if they exist, put an OK, if they don't exist, send me an email, but since there are 4 files, I don't want them to I sent 4 emails (1 for each file) so I want to compress everything in a macro if possible, but I don't know how to reference the 4 files for the day that marks &period if inside the fileexist I can only put a macro variable.
Can someone help me with this? I hope I explained myself well, thank you very much in advance
Maybe something like this:
%macro codeYouWantToDoIf4FilesExist();
/* ... YOUR CODE ... */
%mend codeYouWantToDoIf4FilesExist;
%macro test();
%if %sysfunc(fileexist(&myfilerf1))
AND %sysfunc(fileexist(&myfilerf2))
AND %sysfunc(fileexist(&myfilerf3))
AND %sysfunc(fileexist(&myfilerf4))
%then
%do;
%codeYouWantToDoIf4FilesExist()
%end;
%else %do;
/* code with e-mail */
%end;
%mend test;
%test()
All the best
Bart
Hi,
I macro expression can use the AND operator, so you could do something like:
%if %sysfunc(fileexist(&myfilerf1))
and %sysfunc(fileexist(&myfilerf2))
and %sysfunc(fileexist(&myfilerf3))
and %sysfunc(fileexist(&myfilerf4))
%then %do;
*... ;
%end;
Maybe something like this:
%macro codeYouWantToDoIf4FilesExist();
/* ... YOUR CODE ... */
%mend codeYouWantToDoIf4FilesExist;
%macro test();
%if %sysfunc(fileexist(&myfilerf1))
AND %sysfunc(fileexist(&myfilerf2))
AND %sysfunc(fileexist(&myfilerf3))
AND %sysfunc(fileexist(&myfilerf4))
%then
%do;
%codeYouWantToDoIf4FilesExist()
%end;
%else %do;
/* code with e-mail */
%end;
%mend test;
%test()
All the best
Bart
Another way:
%let myfilerf1=/opt/sas/data/test1_&period..csv;
%let myfilerf2=/opt/sas/data/test2_&period..csv;
%let myfilerf3=/opt/sas/data/test3_&period..csv;
%let myfilerf4=/opt/sas/data/test4_&period..csv;
%global StopExecuting;
%let StopExecuting = 0;
%macro CheckFiles(VarPrefix=);
data _null_;
set sashelp.vmacro(where= (Name =: "%upcase(&VarPrefix.)"));
length FileToTest $ 250;
FileToTest = symget(Name);
if not fileexist(FileToTest) then do;
put 'ERROR: File ' FileToTest ' does not exist.';
call symputx('StopExecuting', 1);
end;
run;
%mend;
%CheckFiles(VarPrefix= myfilerf);
%macro PROD1;
%if &StopExecuting %then %return;
/* Anything you want to do if the file exists */
%mend PROD1;
/* ... More macros and macro calls */
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.