BookmarkSubscribeRSS Feed
Chang
Quartz | Level 8

Hi,

I am a R programmer recapping SAS programming. I usually check the existence of folders and files in the beginning of my R program. I am looking to do the same in my SAS program. I found a working SAS macro that checks the existence of a single path. In my example, it is called %check_existence_single_directory. I've used the macro to check the existence of 3 folders that are pre-existing. All of them are found with no problems. I then attempt to tweak the macro as %check_existence_directory in my example,  to include all the directories I want to check in the macro parameter. This macro is working on the first two paths, but not on the last path which contains special characters period and dashes. It ends up creating new folders. Any idea how I can change the macro or specify the paths that SAS won't stop reading when it encounters a period? 

 

Copying my code and log

 

 

/*Set up local directory*/
%let drive_E= E:\Lab_MarkS\lunC_work ;
%let folder_path_SAS_macro= &drive_E.\library_genetics_epidemiology\SAS_macros_chang ;
%let folder_path_Exp108= &drive_E.\Immunohistochemistry_images\data_output\AP_Exp108.1_PeterMac-lungCancer-CD8-PD1\analysis-results ;
/*SAS macro to check existence of a single directory (working)*/
%macro check_existence_single_directory(dir=) ; 
   *options noxwait; 
   %local rc fileref ; 
   %let rc = %sysfunc(filename(fileref,&dir)) ; 
   %if %sysfunc(fexist(&fileref))  %then 
      %put NOTE: The directory "&dir" exists ; 
   %else 
     %do ; 
         %sysexec md   &dir ; 
         %put %sysfunc(sysmsg()) The directory has been created. ; 
   %end ; 
   %let rc=%sysfunc(filename(fileref)) ; 
%mend check_existence_single_directory ;

/*Check existence of folders*/
%check_existence_single_directory(dir=&drive_E.);
%check_existence_single_directory(dir=&folder_path_SAS_macro.);
%check_existence_single_directory(dir=&folder_path_Exp108.);

Here is the log for the code above

 

NOTE: The directory "E:\Lab_MarkS\lunC_work" exists
291  %check_existence_single_director(dir=&folder_path_SAS_macro.);
NOTE: The directory "E:\Lab_MarkS\lunC_work\library_genetics_epidemiology\SAS_macros_chang"
exists
292  %check_existence_single_director(dir=&folder_path_Exp108.);
NOTE: The directory
"E:\Lab_MarkS\lunC_work\Immunohistochemistry_images\data_output\AP_Exp108.1_PeterMac-lungCancer
-CD8-PD1\analysis-results" exists

Here is the macro I modifed (not working on the third path)

 

 

/*SAS macro to check existence of multiple directories (working?)*/
%macro check_existence_directory(paths=) ; 
   *options noxwait; 
   %local rc fileref ; 
   /*Loop thru each macro parameter value using do loop*/
   %do i=1 %to %sysfunc(countw(&paths.));
   	/*Assign current directory to a macro variable path*/
   	%let path=%scan(&paths.,&i.);
	/*Assign the external path to a file reference then to a new macro variable rc(?)*/
	%let rc = %sysfunc(filename(fileref,&path.)) ;
	%if %sysfunc(fexist(&fileref))  %then 
      %put NOTE: The directory "&path." exists ;
	%else
		%do ;
		/*If the path is not found, create that directory*/
			%sysexec md   &path. ;
			%put %sysfunc(sysmsg()) The directory has been created. ;
		%end; 
	%let rc=%sysfunc(filename(fileref)) ;	
   %end; /*Close the do loop*/
%mend check_existence_directory ;

/*Calling the macro to check the 3 paths*/
%check_existence_directory(paths=&drive_E. &folder_path_SAS_macro. &folder_path_Exp108.) ; 
%check_existence_directory(paths= %STR(&drive_E. &folder_path_SAS_macro. &folder_path_Exp108.)); 

Here is the log for the code above

 

 

NOTE: The directory "E:\Lab_MarkS\lunC_work" exists
NOTE: The directory "E:\Lab_MarkS\lunC_work\library_genetics_epidemiology\SAS_macros_chang"
exists
WARNING: Physical file does not exist,
E:\Lab_MarkS\lunC_work\Immunohistochemistry_images\data_output\AP_Exp108. The directory has
been created.
WARNING: Physical file does not exist, C:\Users\lunC\1_PeterMac. The directory has been
created.
WARNING: Physical file does not exist, C:\Users\lunC\lungCancer. The directory has been
created.
WARNING: Physical file does not exist, C:\Users\lunC\CD8. The directory has been created.
WARNING: Physical file does not exist, C:\Users\lunC\PD1\analysis. The directory has been
created.
WARNING: Physical file does not exist, C:\Users\lunC\results. The directory has been created.

Apparently, the third path is incorrectly read by SAS. How do I improve that? Paths are created according to the group's need. Don't expect non-programmers to know why it is a bad idea to include special characters in the folder.

 

Thanks

 

 

 

 

 

 

 

2 REPLIES 2
ChrisNZ
Tourmaline | Level 20

When you use these functions,

   %do i=1 %to %sysfunc(countw(&paths.));
     %let path=%scan(&paths.,&i.);

dashes and hyphen are delimiters.

 

You can specify delimiters as needed, for example

   %do i=1 %to %sysfunc(countw(&paths.,%str( )));
   	/*Assign current directory to a macro variable path*/
   	%let path=%scan(&paths.,&i.,%str( ));

to specify that only a space is considered the delimiter.

 

 

ChrisNZ
Tourmaline | Level 20

Why are there 2 identical posts? And why under 2 different names?

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 2 replies
  • 481 views
  • 0 likes
  • 2 in conversation