<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Check existence of multiple directories and mask special characters in the path in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Check-existence-of-multiple-directories-and-mask-special/m-p/768211#M243645</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;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&amp;nbsp;&lt;CODE class=" language-sas"&gt;%check_existence_single_directory&lt;/CODE&gt;&amp;nbsp; is&amp;nbsp;working.&amp;nbsp;The&amp;nbsp;macro %check_existence_directory, tweaked from the macro, is probably working. My folder paths contain special characters, such dots and dashes. My questions&lt;/P&gt;&lt;P&gt;- How do I mask these characters in the paths?&lt;/P&gt;&lt;P&gt;- What does this line do?&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;%let rc=%sysfunc(filename(fileref)) ;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;- If you are able to spot any problems with the macro %check_existence_directory, please let me know how it can be improved.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*Set up local directory*/
%let drive_E= E:\Lab_MarkS\lunC_work ;
%let folder_path_SAS_macro= &amp;amp;drive_E.\library_genetics_epidemiology\SAS_macros_chang ;
%let folder_path_Exp108= &amp;amp;drive_E.\Immunohistochemistry_images\data_output\AP_Exp108.1_PeterMac-lungCancer-CD8-PD1\analysis-results ;
&lt;BR /&gt;/*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,&amp;amp;dir)) ; 
   %if %sysfunc(fexist(&amp;amp;fileref))  %then 
      %put NOTE: The directory "&amp;amp;dir" exists ; 
   %else 
     %do ; 
         %sysexec md   &amp;amp;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=&amp;amp;drive_E.);
*/&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*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(&amp;amp;paths.));
   	/*Assign current directory to a macro variable path*/
   	%let path=%scan(&amp;amp;paths.,&amp;amp;i.);
	/*Assign the external path to a file reference then to a new macro variable rc(?)*/
	%let rc = %sysfunc(filename(fileref,&amp;amp;path.)) ;
	%if %sysfunc(fexist(&amp;amp;fileref))  %then 
      %put NOTE: The directory "&amp;amp;path." exists ;
	%else
		%do ;
		/*If the path is not found, create that directory*/
			%sysexec md   &amp;amp;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=&amp;amp;drive_E. &amp;amp;folder_path_SAS_macro. &amp;amp;folder_path_Exp108.) ; 

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Chang&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 17 Sep 2021 01:54:09 GMT</pubDate>
    <dc:creator>luenhchang2</dc:creator>
    <dc:date>2021-09-17T01:54:09Z</dc:date>
    <item>
      <title>Check existence of multiple directories and mask special characters in the path</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-existence-of-multiple-directories-and-mask-special/m-p/768211#M243645</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;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&amp;nbsp;&lt;CODE class=" language-sas"&gt;%check_existence_single_directory&lt;/CODE&gt;&amp;nbsp; is&amp;nbsp;working.&amp;nbsp;The&amp;nbsp;macro %check_existence_directory, tweaked from the macro, is probably working. My folder paths contain special characters, such dots and dashes. My questions&lt;/P&gt;&lt;P&gt;- How do I mask these characters in the paths?&lt;/P&gt;&lt;P&gt;- What does this line do?&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;%let rc=%sysfunc(filename(fileref)) ;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;- If you are able to spot any problems with the macro %check_existence_directory, please let me know how it can be improved.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*Set up local directory*/
%let drive_E= E:\Lab_MarkS\lunC_work ;
%let folder_path_SAS_macro= &amp;amp;drive_E.\library_genetics_epidemiology\SAS_macros_chang ;
%let folder_path_Exp108= &amp;amp;drive_E.\Immunohistochemistry_images\data_output\AP_Exp108.1_PeterMac-lungCancer-CD8-PD1\analysis-results ;
&lt;BR /&gt;/*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,&amp;amp;dir)) ; 
   %if %sysfunc(fexist(&amp;amp;fileref))  %then 
      %put NOTE: The directory "&amp;amp;dir" exists ; 
   %else 
     %do ; 
         %sysexec md   &amp;amp;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=&amp;amp;drive_E.);
*/&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*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(&amp;amp;paths.));
   	/*Assign current directory to a macro variable path*/
   	%let path=%scan(&amp;amp;paths.,&amp;amp;i.);
	/*Assign the external path to a file reference then to a new macro variable rc(?)*/
	%let rc = %sysfunc(filename(fileref,&amp;amp;path.)) ;
	%if %sysfunc(fexist(&amp;amp;fileref))  %then 
      %put NOTE: The directory "&amp;amp;path." exists ;
	%else
		%do ;
		/*If the path is not found, create that directory*/
			%sysexec md   &amp;amp;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=&amp;amp;drive_E. &amp;amp;folder_path_SAS_macro. &amp;amp;folder_path_Exp108.) ; 

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Chang&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Sep 2021 01:54:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-existence-of-multiple-directories-and-mask-special/m-p/768211#M243645</guid>
      <dc:creator>luenhchang2</dc:creator>
      <dc:date>2021-09-17T01:54:09Z</dc:date>
    </item>
    <item>
      <title>Re: Check existence of multiple directories and mask special characters in the path</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-existence-of-multiple-directories-and-mask-special/m-p/768288#M243673</link>
      <description>&lt;P&gt;Is the goal to CREATE the directory if it does not exist?&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;If you have commas and parentheses your paths you might need to macro quote the values in the call. Or add physical quotes.&amp;nbsp; If the paths are already in macro variables like your example I would use %SUPERQ() to add the quoting.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%check_existence_directory(paths
=%superq(drive_E) 
|%superq(folder_path_SAS_macro)
|%superq(folder_path_Exp108)
) ; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you can use the %QSCAN() macro function to add macro quoting to the parsed string.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do i=1 %to %sysfunc(countw(&amp;amp;paths,|));
  %let path=%qscan(&amp;amp;paths,&amp;amp;i,|);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;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.&amp;nbsp;&amp;nbsp;The FILEREF() function will test if the string is a existing fileref.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Sep 2021 13:49:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-existence-of-multiple-directories-and-mask-special/m-p/768288#M243673</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-09-17T13:49:54Z</dc:date>
    </item>
  </channel>
</rss>

