Macro PIPE Command with Wildcard

Occasional Contributor
Posts: 6

Macro PIPE Command with Wildcard

Hello - I'm trying to code a PIPE statement into a macro. The PIPE statement includes a macro variable name (&pathnm) surrounded by wildcard characters (*). When trying the lines below, I get "ERROR: Open code statement recursion detected." Where is  my syntax wrong? Thanks for any tips you can provide.


I did search in the community for similar topics, and I tried surrounding the piece with an extra set of quotes (version 2 further down).


filename output pipe "ls -a /mypath/subfolder/*&pathnm*.csv" ;

Version 2 - same error:

filename output pipe "ls -a ""/mypath/subfolder/*&pathnm*.csv"" " ;
Super User
Posts: 13,942

Re: Macro PIPE Command with Wildcard

Can you show the working command with the same value but not macro variable?

Occasional Contributor
Posts: 6

Re: Macro PIPE Command with Wildcard

filename output pipe 'ls -a /mypath/subfolder/*REJECTED*POLICY*.csv';

Occasional Contributor
Posts: 6

Re: Macro PIPE Command with Wildcard

And the above working version is outside the macro

Super User
Posts: 13,942

Re: Macro PIPE Command with Wildcard

I would try


filename output pipe "ls -a /mypath/subfolder/*&pathnm.*.csv"


without the . immediately after your macro variable SAS is likely looking for a macro variable named &pathnm*  which is not a valid macro variable name. The dot indicates the resolved value  of the macro variable is to be concatenated with the following characters.

Typically we get a "macro variable not found" sort of error message but the * may be treated differently resulting in the "open code recursion" error message.

Super User
Super User
Posts: 8,287

Re: Macro PIPE Command with Wildcard

Are you sure your macro variable PATHNM exists?  The recursive reference message is sometimes related to that as when the SAS macro processor sees that the macro variable doesn't exist then it just leaves the reference, including the leading & character, in place for SAS to deal with.


But usually when trying to use * wild card in generating a unix path it is the presence of the /* that causes a lot of trouble as it looks like an opening of a block comment.  That can end up "eating" a lot of you code and causing all kinds of error message in later pieces of code just because the beginning of the code got cut off by the comment block.


You could try adding macro quoting around one of those two characters.


You can also use the QUOTE() function to add the quotes (and double any embedded quotes in your macro variable).


filename filelist pipe %sysfunc(quote(ls -a /mypath/subfolder/%str(*)&pathnm%str(*).csv,"'")) ;

You could change your Unix command so that the /* is not needed.

filename filelist pipe %sysfunc(quote(cd /mypath/subfolder/; ls -a *&pathnm*.csv,"'")) ;

But then you will need to add the prefix back to the generated list of filenames.

Super User
Posts: 10,601

Re: Macro PIPE Command with Wildcard

You have something in your macro before that code that causes the problem.

See this short example:

%let pathnm=myfiles;
%macro mymac;
filename output pipe "ls -a /mypath/subfolder/*&pathnm*.csv";

filename output list;

The log from that:

27         %let pathnm=myfiles;
28         %macro mymac;
29         filename output pipe "ls -a /mypath/subfolder/*&pathnm*.csv";
30         %mend;
31         %mymac
33         filename output list;
      Physical Name= ls -a /mypath/subfolder/*myfiles*.csv

So you can see that the filename statement works perfectly well in a macro on its own. Reduce your macro to this, and then expand it step-by-step until the problem reappears.

Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Ask a Question
Discussion stats
  • 6 replies
  • 4 in conversation