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
... View more