<?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: Need help understanding macro code in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Need-help-understanding-macro-code/m-p/78310#M16948</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I think you are confused by this line " %let filrf=mydir;". This line is useless in the example. &lt;/P&gt;&lt;P&gt;filename is a sas function. read it from the link &lt;A href="http://support.sas.com/documentation/cdl/en/hostwin/63285/HTML/default/viewer.htm#win-func-filename.htm" title="http://support.sas.com/documentation/cdl/en/hostwin/63285/HTML/default/viewer.htm#win-func-filename.htm"&gt;http://support.sas.com/documentation/cdl/en/hostwin/63285/HTML/default/viewer.htm#win-func-filename.htm&lt;/A&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 26 Sep 2012 20:39:05 GMT</pubDate>
    <dc:creator>Linlin</dc:creator>
    <dc:date>2012-09-26T20:39:05Z</dc:date>
    <item>
      <title>Need help understanding macro code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-understanding-macro-code/m-p/78309#M16947</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I found the code below to scan a directory and find all the files with a certain extension.&amp;nbsp; I am a bit confused with some of the code in the beginning though, specifically the purpose of filrf and mydir.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Specific areas I am confused are the macro variables below.&amp;nbsp; I don't quite understand why filrf is set to mydir, is mydir a predefined SAS keyword?&amp;nbsp; Proc Google did not turn up any information on it so I am not sure.&amp;nbsp; Also, I am not so sure about the next two macro variables.&amp;nbsp; With RC, the filename function has the first parameter as filrf but not the macro variable, just the word filrf.&amp;nbsp; Then the macro variable did is assigned to dopen(&amp;amp;filrf) which is actually dopen(mydir), how does this know which directory to actually open?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If I just run the small segment of code I have below (before the main code) it does not return anything.&amp;nbsp; When I set a macro variable "dir" to a directory, it does return values.&amp;nbsp; I am really confused as to how this is working, if anyone could provide some insight I would really appreciate it.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Segment of code:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: monospace;"&gt;&amp;nbsp; %let filrf=mydir;&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: monospace;"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="font-family: monospace;"&gt;%let rc=%sysfunc(filename(filrf,&amp;amp;dir));&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: monospace;"&gt;&amp;nbsp; %let did=%sysfunc(dopen(&amp;amp;filrf));&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Source:&lt;/P&gt;&lt;P&gt;&lt;A href="http://support.sas.com/kb/25/074.html" title="http://support.sas.com/kb/25/074.html"&gt;http://support.sas.com/kb/25/074.html&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Full Code:&lt;/P&gt;&lt;P&gt;&lt;CODE style="padding: 10px 0 8px;"&gt;%macro drive(dir,ext);&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;&amp;nbsp; %let filrf=mydir;&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;&amp;nbsp; /* Assigns the fileref of mydir to the directory and opens the directory */&amp;nbsp; &lt;BR /&gt;&amp;nbsp; %let rc=%sysfunc(filename(filrf,&amp;amp;dir));&amp;nbsp; &lt;BR /&gt;&amp;nbsp; %let did=%sysfunc(dopen(&amp;amp;filrf));&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;&amp;nbsp; /* Returns the number of members in the directory */&amp;nbsp; &lt;BR /&gt;&amp;nbsp; %let memcnt=%sysfunc(dnum(&amp;amp;did));&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;&amp;nbsp; /* Loops through entire directory */&amp;nbsp; &lt;BR /&gt;&amp;nbsp; %do i = 1 %to &amp;amp;memcnt;&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;&amp;nbsp; /* Returns the extension from each file */&amp;nbsp; &lt;BR /&gt;&amp;nbsp; %let name=%qscan(%qsysfunc(dread(&amp;amp;did,&amp;amp;i)),-1,.);&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;&amp;nbsp; /* Checks to see if file contains an extension */&amp;nbsp; &lt;BR /&gt;&amp;nbsp; %if %qupcase(%qsysfunc(dread(&amp;amp;did,&amp;amp;i))) ne %qupcase(&amp;amp;name) %then %do;&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;&amp;nbsp; /* Checks to see if the extension matches the parameter value */&amp;nbsp; &lt;BR /&gt;&amp;nbsp; /* If condition is true prints the full name to the log */&amp;nbsp; &lt;BR /&gt;&amp;nbsp; %if (%superq(ext) ne and %qupcase(&amp;amp;name) = %qupcase(&amp;amp;ext)) or&amp;nbsp; &lt;BR /&gt;&amp;nbsp; (%superq(ext) = and %superq(name) ne) %then %do;&amp;nbsp; &lt;BR /&gt;&amp;nbsp; %put %qsysfunc(dread(&amp;amp;did,&amp;amp;i));&amp;nbsp; &lt;BR /&gt;&amp;nbsp; %end;&amp;nbsp; &lt;BR /&gt;&amp;nbsp; %end;&amp;nbsp; &lt;BR /&gt;&amp;nbsp; %end;&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;&amp;nbsp; /* Closes the directory */&amp;nbsp; &lt;BR /&gt;&amp;nbsp; %let rc=%sysfunc(dclose(&amp;amp;did));&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;%mend drive;&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;/* First parameter is the directory of where your files are stored. */&amp;nbsp; &lt;BR /&gt;/* Second parameter is the extension you are looking for. */&amp;nbsp; &lt;BR /&gt;/* Leave 2nd paramater blank if you want a list of all the files. */&amp;nbsp; &lt;BR /&gt;%drive(c:\,sas)&lt;/CODE&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 Sep 2012 19:58:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-understanding-macro-code/m-p/78309#M16947</guid>
      <dc:creator>lsirakos</dc:creator>
      <dc:date>2012-09-26T19:58:49Z</dc:date>
    </item>
    <item>
      <title>Re: Need help understanding macro code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-understanding-macro-code/m-p/78310#M16948</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I think you are confused by this line " %let filrf=mydir;". This line is useless in the example. &lt;/P&gt;&lt;P&gt;filename is a sas function. read it from the link &lt;A href="http://support.sas.com/documentation/cdl/en/hostwin/63285/HTML/default/viewer.htm#win-func-filename.htm" title="http://support.sas.com/documentation/cdl/en/hostwin/63285/HTML/default/viewer.htm#win-func-filename.htm"&gt;http://support.sas.com/documentation/cdl/en/hostwin/63285/HTML/default/viewer.htm#win-func-filename.htm&lt;/A&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 Sep 2012 20:39:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-understanding-macro-code/m-p/78310#M16948</guid>
      <dc:creator>Linlin</dc:creator>
      <dc:date>2012-09-26T20:39:05Z</dc:date>
    </item>
    <item>
      <title>Re: Need help understanding macro code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-understanding-macro-code/m-p/78311#M16949</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The way that the FILENAME() function works with SYSFUNC() is that it want the name of the macro variable that will hold the actual fileref to be created.&amp;nbsp; In this case the macro variable FILRF is used. Setting the value to MYDIR before the call to FILENAME() means that it is similar to using this statement:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;FILENAME MYDIR "&amp;amp;dir" ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;When the macro variable is empty then the FILENAME statement will generate a fileref and assign it to the macro variable.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;472&amp;nbsp; %let filrf=;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;473&amp;nbsp; %let rc=%sysfunc(filename(filrf,'test'));&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;474&amp;nbsp; %put rc=&amp;amp;rc filrf=&amp;amp;filrf;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;rc=0 filrf=#LN00053&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 Sep 2012 21:16:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-understanding-macro-code/m-p/78311#M16949</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2012-09-26T21:16:32Z</dc:date>
    </item>
    <item>
      <title>Re: Need help understanding macro code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-understanding-macro-code/m-p/78312#M16950</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;So is there a difference when it is set to mydir or when it is left empty and assigned a fileref like in your example?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The next step is:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%let did=%sysfunc(dopen(&amp;amp;filrf));&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;if filrf is set to mydir, when the code above is called does mydir point to SAS to the directory specified?&amp;nbsp; If filrf is left blank and the FILENAME statement generates a fileref, does that point SAS to the directory specified?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;That is the area where I am getting confused because like in the code you specified, when you use %put filrf=&amp;amp;filrf; it shows #LN00053 (or whatever random value it assigns it).&amp;nbsp; I guess what I am not seeing because it goes on behind the scenes is that value is actually a pointer to the specified directory.&amp;nbsp; Is that correct?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 Sep 2012 21:34:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-understanding-macro-code/m-p/78312#M16950</guid>
      <dc:creator>lsirakos</dc:creator>
      <dc:date>2012-09-26T21:34:32Z</dc:date>
    </item>
    <item>
      <title>Re: Need help understanding macro code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-understanding-macro-code/m-p/78313#M16951</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;FILENAME statement (or function call) creates a fileref that links a nice short alias name to the physical location.&amp;nbsp; &lt;/P&gt;&lt;P&gt;Just like the LIBNAME statement does for data libraries.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In this program the physical location must be a director, otherwise the DOPEN() function call will fail.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I do not think that it matters for this program whether you pick your own value for the fileref&amp;nbsp; or let SAS assign one. The advantage of picking your own is that you can use one that is easier to understand and read, like MYDIR for a fileref that points to directory.&amp;nbsp; The disadvantage is that it might conflict with the use of that same fileref alias for another purpose in another part of the program.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 Sep 2012 21:42:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-understanding-macro-code/m-p/78313#M16951</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2012-09-26T21:42:37Z</dc:date>
    </item>
    <item>
      <title>Re: Need help understanding macro code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-understanding-macro-code/m-p/78314#M16952</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Got it, thank you so much, that makes sense to me now.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 Sep 2012 21:56:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-understanding-macro-code/m-p/78314#M16952</guid>
      <dc:creator>lsirakos</dc:creator>
      <dc:date>2012-09-26T21:56:21Z</dc:date>
    </item>
  </channel>
</rss>

