<?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: output macro variable into dataset under nested loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/output-macro-variable-into-dataset-under-nested-loop/m-p/555841#M154742</link>
    <description>&lt;P&gt;Sadly SAS still doesn't have write functions to data sets.&lt;/P&gt;
&lt;P&gt;So you need something like this:&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;                                                                                                                                 
                          
data OUT; 
length DIR NAME $200;
                                                                                                              
   /* 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;      

DIR="&amp;amp;dir"; NAME="&amp;amp;name"; output;
                                                                                                          
      %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;                  

run;                                                                                                              
                                                                                                                                        
  /* Closes the directory and clear the fileref */                                                                                      
  %let rc=%sysfunc(dclose(&amp;amp;did));                                                                                                       
  %let rc=%sysfunc(filename(filrf));                                                                                                    
                                                                                                                                        
%mend drive;       &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 03 May 2019 05:22:54 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2019-05-03T05:22:54Z</dc:date>
    <item>
      <title>output macro variable into dataset under nested loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-macro-variable-into-dataset-under-nested-loop/m-p/555542#M154612</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;from below code I want to create dataset which will have this two column path and lname which gate value from this two macro variable &amp;amp;dir and &amp;amp;name respective. can you please help me to put my output statement in right position so that I can create data set ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;
&lt;PRE&gt;&lt;CODE&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,sas)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 02 May 2019 07:58:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-macro-variable-into-dataset-under-nested-loop/m-p/555542#M154612</guid>
      <dc:creator>Tushar</dc:creator>
      <dc:date>2019-05-02T07:58:52Z</dc:date>
    </item>
    <item>
      <title>Re: output macro variable into dataset under nested loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-macro-variable-into-dataset-under-nested-loop/m-p/555841#M154742</link>
      <description>&lt;P&gt;Sadly SAS still doesn't have write functions to data sets.&lt;/P&gt;
&lt;P&gt;So you need something like this:&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;                                                                                                                                 
                          
data OUT; 
length DIR NAME $200;
                                                                                                              
   /* 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;      

DIR="&amp;amp;dir"; NAME="&amp;amp;name"; output;
                                                                                                          
      %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;                  

run;                                                                                                              
                                                                                                                                        
  /* Closes the directory and clear the fileref */                                                                                      
  %let rc=%sysfunc(dclose(&amp;amp;did));                                                                                                       
  %let rc=%sysfunc(filename(filrf));                                                                                                    
                                                                                                                                        
%mend drive;       &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 May 2019 05:22:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-macro-variable-into-dataset-under-nested-loop/m-p/555841#M154742</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-05-03T05:22:54Z</dc:date>
    </item>
    <item>
      <title>Re: output macro variable into dataset under nested loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-macro-variable-into-dataset-under-nested-loop/m-p/555849#M154747</link>
      <description>&lt;P&gt;This doesn't do exactly what you want, as it does not support full recursion.&amp;nbsp; Perhaps someone smarter than me (@ChrisNZ?) can get recursion working.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, this does one-level recursion:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc delete data=filelist;
run;
%dirlist(dir=C:\Temp,type=d,data=dirlist)
%macro code;
   %let fullname=%trim(&amp;amp;fullname);
   %dirlist(dir=&amp;amp;fullname,data=temp,filter=ext='sas');
   proc append base=filelist data=temp;
   run;
%mend;
%loop_control(control=dirlist);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;See:&lt;/P&gt;&lt;P&gt;&lt;A href="https://github.com/scottbass/SAS/blob/master/Macro/dirlist.sas" target="_blank"&gt;https://github.com/scottbass/SAS/blob/master/Macro/dirlist.sas&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://github.com/scottbass/SAS/blob/master/Macro/loop_control.sas" target="_blank"&gt;https://github.com/scottbass/SAS/blob/master/Macro/loop.sas&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://github.com/scottbass/SAS/blob/master/Macro/loop_control.sas" target="_blank"&gt;https://github.com/scottbass/SAS/blob/master/Macro/loop_control.sas&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://github.com/scottbass/SAS/blob/master/Macro/loop_control.sas" target="_blank"&gt;https://github.com/scottbass/SAS/blob/master/Macro/parmv.sas&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, I think you'd get &lt;U&gt;&lt;STRONG&gt;much&lt;/STRONG&gt; &lt;/U&gt;better performance if you did most of this code in the data step rather than macro.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Something like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro drive(dir,ext);  
   data dirlist;
      rc=filename("dirlist","&amp;amp;dir");
      did=dopen("dirlist");
      do i=1 to dnum(did);
         name=dread(did,i);
         if scan(name,-1,'.')="&amp;amp;ext" then output;
      end;
      rc=dclose(did);
      rc=filename("dirlist");

      * perhaps implement recursion with link/return statements? ;
   run;
%mend; 
%drive(C:\Temp,sas);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The &lt;U&gt;&lt;STRONG&gt;only&lt;/STRONG&gt; &lt;/U&gt;macro "bits" are the parameters for file path and desired extension - everything else is a data step.&amp;nbsp; If you're looping via macro, that will likely be orders of magnitude slower than the data step.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp;if you do get recursion working, and it can be added to my dirlist macro, that would be great!&amp;nbsp; I just haven't bothered with that for now.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 May 2019 06:17:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-macro-variable-into-dataset-under-nested-loop/m-p/555849#M154747</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2019-05-03T06:17:56Z</dc:date>
    </item>
    <item>
      <title>Re: output macro variable into dataset under nested loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-macro-variable-into-dataset-under-nested-loop/m-p/555854#M154750</link>
      <description>&lt;P&gt;Also, do you have ALLOWXCMD available in your environment?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What happens if you run this code (adjust if you're running Linux):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename dirlist pipe "dir /b /s \\sascs\linkage\RL_content_snapshots";
data dirlist1;
   infile dirlist lrecl=256 truncover;
   input;
   filename=_infile_;
run;

* or ;

filename dirlist pipe "powershell ""Get-ChildItem -recurse \\sascs\linkage\RL_content_snapshots | ForEach-Object {$_.Fullname}"" ";
data dirlist2;
   infile dirlist lrecl=256 truncover;
   input;
   filename=_infile_;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 03 May 2019 07:00:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-macro-variable-into-dataset-under-nested-loop/m-p/555854#M154750</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2019-05-03T07:00:07Z</dc:date>
    </item>
  </channel>
</rss>

