2

I try to export a file in SAS but I get "Open code statement recursion detected." error. Since I export more than one files depending on date I define as a macro variable based on the prompt date, I want to name my file to be exported with this variable but it does not work. I would really appreciate if anyone helps me.

rep_date = 30APR2015:00:00:00
 Outfile = work.A042015.sas7

%let var = CATS("A",MONTH(DATEPART(&rep_date)),YEAR(DATEPART(&rep_date)));
data WORK.&var(compress=yes); 
set WORK.have;
run; 
Jonsi Billups
  • 133
  • 1
  • 3
  • 15
  • So if the macro variable is as specified then you will need to reference it as `"&rep_date"dt` to have SAS interpret it as a datetime literal rather than a random stream of letters and digits. Also dataset refereences in SAS cannot have three levels. You could refer to `WORK.A042015` or just `A042015`. Also I would reverse the order of the Year and Month number so that the dataset names will appear in chronological order when sorted lexically. Also the file extension that SAS uses in the physical file that it generates for SAS datasets is 'sas7bdat'. – Tom Nov 02 '15 at 16:57

2 Answers2

2

Macro variables are just strings. So if you want to execute functions in macro code you need to wrap the function inside of the %SYSFUNC() macro function.

%let rep_date='01JAN2015:01:23'dt ;
%let dsname = A%sysfunc(datepart(&rep_date),monyy6);
data &dsname(compress=yes);
  set have;
run;
Tom
  • 47,574
  • 2
  • 16
  • 29
2

As a more broad issue, OPEN STATEMENT RECURSION DETECTED refers to cases where you assign a macro variable to itself.

%let &mvar = &mvar;

Of course, this wouldn't normally happen on purpose (one would think). When it does happen, usually it's a sign of one of two classes of mistake.

  1. You're missing a semicolon, end parenthesis, end quote, or something else that causes SAS to not "see" the semicolon at the end of the %let statement. Then your next statement uses the macro variable in a macro context, and SAS sees that as part of the %let statement, which causes this error message.
  2. Something went wrong somewhere else, and you have an issue where that something-else propagates errors further down that don't make any sense. Having an unmatched quotation is a classic example of this, as is a macro that doesn't properly %mend.

1 can happen in cases as simple as this:

%let mvar=mydataset
%put &mvar;

Oops. If it's that simple, then just pop the semicolon on and you're good. It could, however, be caused by something more significant - such as an unmatched parenthesis or quotation - which might require restarting your SAS session. (Sometimes submitting a magic string, which are variations on %*;*;*';*";%*%mend;*);, will fix things, sometimes not. Restarting SAS is a reliable way to fix that).

That's also true with 2 above - if a magic string doesn't fix it, then you may simply need to restart your SAS session. Of course, you still need to find that unmatched quote/parenthesis/etc., but you first need to start SAS over so you can figure it out.

Joe
  • 62,789
  • 6
  • 49
  • 67