DATA Step, Macro, Functions and more

Need help understanding macro code

Accepted Solution Solved
Reply
Contributor
Posts: 27
Accepted Solution

Need help understanding macro code

I found the code below to scan a directory and find all the files with a certain extension.  I am a bit confused with some of the code in the beginning though, specifically the purpose of filrf and mydir.

Specific areas I am confused are the macro variables below.  I don't quite understand why filrf is set to mydir, is mydir a predefined SAS keyword?  Proc Google did not turn up any information on it so I am not sure.  Also, I am not so sure about the next two macro variables.  With RC, the filename function has the first parameter as filrf but not the macro variable, just the word filrf.  Then the macro variable did is assigned to dopen(&filrf) which is actually dopen(mydir), how does this know which directory to actually open?

If I just run the small segment of code I have below (before the main code) it does not return anything.  When I set a macro variable "dir" to a directory, it does return values.  I am really confused as to how this is working, if anyone could provide some insight I would really appreciate it.

Segment of code:

  %let filrf=mydir; 

  %let rc=%sysfunc(filename(filrf,&dir)); 

  %let did=%sysfunc(dopen(&filrf));

Source:

http://support.sas.com/kb/25/074.html

Full Code:

%macro drive(dir,ext); 
  
  %let filrf=mydir; 
  
  /* Assigns the fileref of mydir to the directory and opens the directory */ 
  %let rc=%sysfunc(filename(filrf,&dir)); 
  %let did=%sysfunc(dopen(&filrf)); 
  
  /* Returns the number of members in the directory */ 
  %let memcnt=%sysfunc(dnum(&did)); 
  
  /* Loops through entire directory */ 
  %do i = 1 %to &memcnt; 
  
  /* Returns the extension from each file */ 
  %let name=%qscan(%qsysfunc(dread(&did,&i)),-1,.); 
  
  /* Checks to see if file contains an extension */ 
  %if %qupcase(%qsysfunc(dread(&did,&i))) ne %qupcase(&name) %then %do; 
  
  /* Checks to see if the extension matches the parameter value */ 
  /* If condition is true prints the full name to the log */ 
  %if (%superq(ext) ne and %qupcase(&name) = %qupcase(&ext)) or 
  (%superq(ext) = and %superq(name) ne) %then %do; 
  %put %qsysfunc(dread(&did,&i)); 
  %end; 
  %end; 
  %end; 
  
  /* Closes the directory */ 
  %let rc=%sysfunc(dclose(&did)); 
  
%mend drive; 
  
/* First parameter is the directory of where your files are stored. */ 
/* Second parameter is the extension you are looking for. */ 
/* Leave 2nd paramater blank if you want a list of all the files. */ 
%drive(c:\,sas)


Accepted Solutions
Solution
‎09-26-2012 05:42 PM
Super User
Super User
Posts: 7,078

Re: Need help understanding macro code

FILENAME statement (or function call) creates a fileref that links a nice short alias name to the physical location. 

Just like the LIBNAME statement does for data libraries.

In this program the physical location must be a director, otherwise the DOPEN() function call will fail.

I do not think that it matters for this program whether you pick your own value for the fileref  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.  The disadvantage is that it might conflict with the use of that same fileref alias for another purpose in another part of the program.

View solution in original post


All Replies
Super Contributor
Posts: 1,636

Re: Need help understanding macro code

Hi,

I think you are confused by this line " %let filrf=mydir;". This line is useless in the example.

filename is a sas function. read it from the link http://support.sas.com/documentation/cdl/en/hostwin/63285/HTML/default/viewer.htm#win-func-filename....

Super User
Super User
Posts: 7,078

Re: Need help understanding macro code

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.  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:

FILENAME MYDIR "&dir" ;

When the macro variable is empty then the FILENAME statement will generate a fileref and assign it to the macro variable.

472  %let filrf=;

473  %let rc=%sysfunc(filename(filrf,'test'));

474  %put rc=&rc filrf=&filrf;

rc=0 filrf=#LN00053

Contributor
Posts: 27

Re: Need help understanding macro code

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?

The next step is:

%let did=%sysfunc(dopen(&filrf));

if filrf is set to mydir, when the code above is called does mydir point to SAS to the directory specified?  If filrf is left blank and the FILENAME statement generates a fileref, does that point SAS to the directory specified?

That is the area where I am getting confused because like in the code you specified, when you use %put filrf=&filrf; it shows #LN00053 (or whatever random value it assigns it).  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.  Is that correct?

Thanks!

Solution
‎09-26-2012 05:42 PM
Super User
Super User
Posts: 7,078

Re: Need help understanding macro code

FILENAME statement (or function call) creates a fileref that links a nice short alias name to the physical location. 

Just like the LIBNAME statement does for data libraries.

In this program the physical location must be a director, otherwise the DOPEN() function call will fail.

I do not think that it matters for this program whether you pick your own value for the fileref  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.  The disadvantage is that it might conflict with the use of that same fileref alias for another purpose in another part of the program.

Contributor
Posts: 27

Re: Need help understanding macro code

Got it, thank you so much, that makes sense to me now.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 721 views
  • 3 likes
  • 3 in conversation