<?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 Re: How to run a script on several directories? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-run-a-script-on-several-directories/m-p/507573#M136269</link>
    <description>&lt;P&gt;Are you trying to find all the .sas7bdat&amp;nbsp; in a directory and sub-directory and execute a program?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below is the code that used to list all the files withing a directory. Once you get the list then you can&amp;nbsp; execute them.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro drive(dir,ext);                                                                                                                  
  %local filrf rc did memcnt name i;                                                                                                    
                                                                                                                                        
  /* Assigns a fileref to the directory and opens the directory */                                                           
  %let rc=%sysfunc(filename(filrf,&amp;amp;dir));                                                                                               
  %let did=%sysfunc(dopen(&amp;amp;filrf));                                                                                                     
                                                                                                                                        
  /* Make sure directory can be open */                                                                                                 
  %if &amp;amp;did eq 0 %then %do;                                                                                                              
   %put Directory &amp;amp;dir cannot be open or does not exist;                                                                                
   %return;                                                                                                                             
  %end;                                                                                                                                 
                                                                                                                                        
   /* Loops through entire directory */                                                                                                 
   %do i = 1 %to %sysfunc(dnum(&amp;amp;did));                                                                                                  
                                                                                                                                        
     /* Retrieve name of each file */                                                                                                   
     %let name=%qsysfunc(dread(&amp;amp;did,&amp;amp;i));                                                                                               
                                                                                                                                        
     /* Checks to see if the extension matches the parameter value */                                                                   
     /* If condition is true print the full name to the log        */                                                                   
      %if %qupcase(%qscan(&amp;amp;name,-1,.)) = %upcase(&amp;amp;ext) %then %do;                                                                       
        %put &amp;amp;dir\&amp;amp;name;                                                                                                                
      %end;                                                                                                                             
     /* If directory name call macro again */                                                                                           
      %else %if %qscan(&amp;amp;name,2,.) = %then %do;                                                                                          
        %drive(&amp;amp;dir\%unquote(&amp;amp;name),&amp;amp;ext)                                                                                               
      %end;                                                                                                                             
                                                                                                                                        
   %end;                                                                                                                                
                                                                                                                                        
  /* Closes the directory and clear the fileref */                                                                                      
  %let rc=%sysfunc(dclose(&amp;amp;did));                                                                                                       
  %let rc=%sysfunc(filename(filrf));                                                                                                    
                                                                                                                                        
%mend drive;                                                                                                                            
                                                                                                                                        
/* First parameter is the directory of where your files are stored. */                                                                  
/* Second parameter is the extension you are looking for.           */                                                                  
%drive(c:\temp,sas7bdat) &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 25 Oct 2018 20:32:18 GMT</pubDate>
    <dc:creator>SuryaKiran</dc:creator>
    <dc:date>2018-10-25T20:32:18Z</dc:date>
    <item>
      <title>How to run a script on several directories?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-run-a-script-on-several-directories/m-p/507561#M136263</link>
      <description>&lt;P&gt;Hi!&lt;/P&gt;&lt;P&gt;I have several directories (50) with data files and I would like to run the same script on all of them without editing directory path for each directory. How&amp;nbsp;could I do that? What would be the code?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Using SAS 9.4. (English)&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Anna&lt;/P&gt;</description>
      <pubDate>Thu, 25 Oct 2018 20:03:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-run-a-script-on-several-directories/m-p/507561#M136263</guid>
      <dc:creator>annapechenina</dc:creator>
      <dc:date>2018-10-25T20:03:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to run a script on several directories?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-run-a-script-on-several-directories/m-p/507565#M136264</link>
      <description>What kind of script? Is it a macro, stored process, a SAS program? How would you call it for a single folder? We need a lot more details. It could be as simple as using a macro here to list all files and then use CALL EXECUTE to run the macro for each file. If it's reading all files in for example, you could just create a list of the locations and import all at once (if same format) or import them each into their own data. &lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 25 Oct 2018 20:12:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-run-a-script-on-several-directories/m-p/507565#M136264</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-10-25T20:12:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to run a script on several directories?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-run-a-script-on-several-directories/m-p/507569#M136266</link>
      <description>&lt;P&gt;Thank you for your questions, Reeza!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It is a SAS program and it performs manipulations with data sets within a directory. How would I tell SAS to go to run this script on each directory I have? Now I have to manually change the path within the script. I would like to automate this process.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am open to non SAS solutions like powershell or command line.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Oct 2018 20:20:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-run-a-script-on-several-directories/m-p/507569#M136266</guid>
      <dc:creator>annapechenina</dc:creator>
      <dc:date>2018-10-25T20:20:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to run a script on several directories?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-run-a-script-on-several-directories/m-p/507571#M136267</link>
      <description>&lt;P&gt;Are the "files" SAS data sets?&lt;/P&gt;
&lt;P&gt;A SAS library can reference multiple directories, though I would say 50 at a time might be problematic due to possible length and the response to the following question:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do any of the files share the same name other than being in different directories?&lt;/P&gt;</description>
      <pubDate>Thu, 25 Oct 2018 20:22:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-run-a-script-on-several-directories/m-p/507571#M136267</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-10-25T20:22:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to run a script on several directories?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-run-a-script-on-several-directories/m-p/507572#M136268</link>
      <description>&lt;P&gt;ballardw,&lt;/P&gt;&lt;P&gt;all the files are "SAS" data sets and they have the same names, the only thing different is directory name.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Oct 2018 20:26:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-run-a-script-on-several-directories/m-p/507572#M136268</guid>
      <dc:creator>annapechenina</dc:creator>
      <dc:date>2018-10-25T20:26:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to run a script on several directories?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-run-a-script-on-several-directories/m-p/507573#M136269</link>
      <description>&lt;P&gt;Are you trying to find all the .sas7bdat&amp;nbsp; in a directory and sub-directory and execute a program?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below is the code that used to list all the files withing a directory. Once you get the list then you can&amp;nbsp; execute them.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro drive(dir,ext);                                                                                                                  
  %local filrf rc did memcnt name i;                                                                                                    
                                                                                                                                        
  /* Assigns a fileref to the directory and opens the directory */                                                           
  %let rc=%sysfunc(filename(filrf,&amp;amp;dir));                                                                                               
  %let did=%sysfunc(dopen(&amp;amp;filrf));                                                                                                     
                                                                                                                                        
  /* Make sure directory can be open */                                                                                                 
  %if &amp;amp;did eq 0 %then %do;                                                                                                              
   %put Directory &amp;amp;dir cannot be open or does not exist;                                                                                
   %return;                                                                                                                             
  %end;                                                                                                                                 
                                                                                                                                        
   /* Loops through entire directory */                                                                                                 
   %do i = 1 %to %sysfunc(dnum(&amp;amp;did));                                                                                                  
                                                                                                                                        
     /* Retrieve name of each file */                                                                                                   
     %let name=%qsysfunc(dread(&amp;amp;did,&amp;amp;i));                                                                                               
                                                                                                                                        
     /* Checks to see if the extension matches the parameter value */                                                                   
     /* If condition is true print the full name to the log        */                                                                   
      %if %qupcase(%qscan(&amp;amp;name,-1,.)) = %upcase(&amp;amp;ext) %then %do;                                                                       
        %put &amp;amp;dir\&amp;amp;name;                                                                                                                
      %end;                                                                                                                             
     /* If directory name call macro again */                                                                                           
      %else %if %qscan(&amp;amp;name,2,.) = %then %do;                                                                                          
        %drive(&amp;amp;dir\%unquote(&amp;amp;name),&amp;amp;ext)                                                                                               
      %end;                                                                                                                             
                                                                                                                                        
   %end;                                                                                                                                
                                                                                                                                        
  /* Closes the directory and clear the fileref */                                                                                      
  %let rc=%sysfunc(dclose(&amp;amp;did));                                                                                                       
  %let rc=%sysfunc(filename(filrf));                                                                                                    
                                                                                                                                        
%mend drive;                                                                                                                            
                                                                                                                                        
/* First parameter is the directory of where your files are stored. */                                                                  
/* Second parameter is the extension you are looking for.           */                                                                  
%drive(c:\temp,sas7bdat) &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Oct 2018 20:32:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-run-a-script-on-several-directories/m-p/507573#M136269</guid>
      <dc:creator>SuryaKiran</dc:creator>
      <dc:date>2018-10-25T20:32:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to run a script on several directories?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-run-a-script-on-several-directories/m-p/507575#M136270</link>
      <description>&lt;P&gt;I don't know what you mean by "script".&amp;nbsp; Normally you call a file with SAS code in it a program. How easy it is to convert to working on different directories depends a lot on what exactly it does.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let's just create a trivial example of a program. One that just runs a LIBNAME statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;libname mydata 'C:\mydata';&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To prepare to make it dynamic change the hard coded directory specification into a macro variable reference.&lt;/P&gt;
&lt;P&gt;Remember that macro triggers ( &amp;amp; % ) are not processed inside single quotes.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let mydir=C:\mydata;
libname mydata "&amp;amp;mydir.";&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The next step it so convert the logic that processes one directory into a macro.&amp;nbsp; You can change your macro variable into a parameter to the macro instead. And then pass in the actual directory path when you call the macro.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro mymacro(mydir);
libname mydata "&amp;amp;mydir.";
%mend mymacro;

%mymacro(C:\mydata)
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now you could just replicate that macro call 50 times and type in the other directory names.&lt;/P&gt;
&lt;P&gt;Or if you have the list of directories in a dataset then use that to generate the macro calls.&lt;/P&gt;
&lt;P&gt;So for example if you had a dataset named MYLIST with a variable named MYDIR you could use this data step to generate one macro call for each observation.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set mylist;
  call execute(cats('%nrstr(mymacro)(',mydir,')'));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The %NRSTR() will prevent SAS from trying the expand the macro when CALL EXECUTE pushes the code onto the stack to run after the data step.&amp;nbsp; Instead the macro will be called when SAS pulls the stored lines of code back off of the stack generated by the CALL EXECUTE() statements running.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The next level of automation is to generate the list of directories automatically.&amp;nbsp; Usually that is most easily done if you can access operating system commands.&amp;nbsp; For example on a PC you might read the results of a DIR commmand.&amp;nbsp; Or on Unix you might read the results of a ls or find command.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data mylist;
  infile 'dir /b/ad c:\mydata\*' pipe truncover;
  input mydir $256. ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Oct 2018 21:57:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-run-a-script-on-several-directories/m-p/507575#M136270</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-10-25T21:57:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to run a script on several directories?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-run-a-script-on-several-directories/m-p/507578#M136271</link>
      <description>&lt;P&gt;You can list all data-sets having same name and use a loop to execute your script supplying the libname into call execute.&lt;/P&gt;
&lt;P&gt;Just a hint:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table list_of_libraries
  as select libname  /* , memname  */
  where memname = &amp;lt;dataset_name&amp;gt;
  from dictionary.tables;
quit.&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 25 Oct 2018 20:53:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-run-a-script-on-several-directories/m-p/507578#M136271</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2018-10-25T20:53:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to run a script on several directories?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-run-a-script-on-several-directories/m-p/507601#M136274</link>
      <description>&lt;P&gt;Yes!! This is it! Thank you very much! I will report back after I test it.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Oct 2018 21:36:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-run-a-script-on-several-directories/m-p/507601#M136274</guid>
      <dc:creator>annapechenina</dc:creator>
      <dc:date>2018-10-25T21:36:58Z</dc:date>
    </item>
  </channel>
</rss>

