BookmarkSubscribeRSS Feed
luenhchang2
Calcite | Level 5

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. I have attempted to tweak the macro to include multiple directories in the macro parameter. The macro %check_existence_single_directory  is working. The macro %check_existence_directory, tweaked from the macro, is probably working. My folder paths contain special characters, such dots and dashes. My questions

- How do I mask these characters in the paths?

- What does this line do?

%let rc=%sysfunc(filename(fileref)) ;

- If you are able to spot any problems with the macro %check_existence_directory, please let me know how it can be improved.

 

Thanks

 

/*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 ; /*An example of running the macro %include "E:\Lab_MarkS\lunC_work\library_genetics_epidemiology\SAS_macros_chang\check_directory_exist.sas"; %check_existence_single_directory(dir=&drive_E.); */

 

 

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

%check_existence_directory(paths=&drive_E. &folder_path_SAS_macro. &folder_path_Exp108.) ; 

Chang

 

1 REPLY 1
Tom
Super User Tom
Super User

Is the goal to CREATE the directory if it does not exist?

Since space is (unfortunately) now a common character used in filenames I would suggest using some character that is invalid for use in a filename, like | , as the delimiter in your macro call with multiple filenames.

If you have commas and parentheses your paths you might need to macro quote the values in the call. Or add physical quotes.  If the paths are already in macro variables like your example I would use %SUPERQ() to add the quoting.

%check_existence_directory(paths
=%superq(drive_E) 
|%superq(folder_path_SAS_macro)
|%superq(folder_path_Exp108)
) ; 

Then you can use the %QSCAN() macro function to add macro quoting to the parsed string.

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

In SAS code you either refer to a file using a quoted physical path or you could use the FILENAME statement to define a short one word name, fileref, that you could use to refer to the file.  The FILEREF() function will test if the string is a existing fileref.  

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 1 reply
  • 470 views
  • 0 likes
  • 2 in conversation