BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
lsirakos
Calcite | Level 5

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)

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

5 REPLIES 5
Linlin
Lapis Lazuli | Level 10

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

Tom
Super User Tom
Super User

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

lsirakos
Calcite | Level 5

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!

Tom
Super User Tom
Super User

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.

lsirakos
Calcite | Level 5

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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