BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Abelp9
Quartz | Level 8

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

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

3 REPLIES 3
Quentin
Super User

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;  
The Boston Area SAS Users Group is hosting free webinars!
Next up: Joe Madden & Joseph Henry present Putting Power into the Hands of the Programmer with SAS Viya Workbench on Wednesday Nov 6.
Register now at https://www.basug.org/events.
yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



andreas_lds
Jade | Level 19

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 */

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

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.

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