BookmarkSubscribeRSS Feed
shl007
Obsidian | Level 7

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"" " ;
6 REPLIES 6
ballardw
Super User

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

shl007
Obsidian | Level 7
filename output pipe 'ls -a /mypath/subfolder/*REJECTED*POLICY*.csv';

shl007
Obsidian | Level 7

And the above working version is outside the macro

ballardw
Super User

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.

Tom
Super User Tom
Super User

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.

Kurt_Bremser
Super User

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";
%mend;
%mymac

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
32         
33         filename output list;
NOTE: Fileref= OUTPUT
      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.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1870 views
  • 5 likes
  • 4 in conversation