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

Hi Team,

 

I am getting an issue when I am delcaring macro variables,

 

data test;
%let %do i=0 %to 24;

%let var1=a&i;
%let var2=b&i;
%let var3=c&i;
%let var4=d&i;
%let var5=e&i;
%let month=period&i;
output;
%end;
run;

This is the error;

 

ERROR: Open code statement recursion detected.

 

Anyone can Help me on this??

 

Thanks a lot in advance

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

%do is only allowed within a macro definition, and no %let should be done within another %let; Since your %do is within a %let, all other macro statements are considered part of that first %let.

 

Bottom line: start with simple code before turning to complex things. And DO NOT get into macros before you have a thorough grasp of programming as such. The macro language needs a programmer's mindset.

View solution in original post

7 REPLIES 7
Astounding
PROC Star

You are trying to use macro language without knowing basic concepts:

 

  • What should a %LET statement look like
  • When is a %DO statement permissible
  • What is the relationship between macro language and SAS language

If you describe what you are trying to accomplish, you can get answers to a specific question.  But the studying part is up to you.

robert215
Calcite | Level 5

Hi ,

 

What should the output dataset test look like ?

novinosrin
Tourmaline | Level 20

Well, You simply cannot declare two macro statements in one i.e %let and %do, these are 2 independent exclusive statements. So that's an absolute NO there.

Open code recursion was simply caused by not finding the end of macro statement ; and if often contributed to be known as black hole problem

What is that you want to accomplish?

Kurt_Bremser
Super User

%do is only allowed within a macro definition, and no %let should be done within another %let; Since your %do is within a %let, all other macro statements are considered part of that first %let.

 

Bottom line: start with simple code before turning to complex things. And DO NOT get into macros before you have a thorough grasp of programming as such. The macro language needs a programmer's mindset.

Jcorti
Obsidian | Level 7

Thanks Team,

 

I did some adjustment to my code based on all your suggestions:

 

%macro vars; 
%let outobs=max;
%do i=0  %to 24;
%let b =&i;
%let var1=test1&b;
%let var2=test2t&b;
%let var3=test3&b;
%let var4=test4t&b;
%let var5=test5&b;
%let var6=test6&b;
%let month=period_&b;

proc  sql;
create table &month as	   
select
filler5
,&var1
,&var2
,&var3
,&var4
,&var5
,&var6
,case when &var1<>-16 or &var2<>-16
or &var3<>-16 or &var4<>-16 or &var5 is not null then 1 else 0 end as
&month

from SAS_Out.&Data_Out
WHERE a='x';
;
run;
%end;
run;

%mend vars;
%vars;

And it worked perfectly.

 

An now can you understand what I am tryng to do??

 

Thanks Again all

 

JC

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Quite frankly no.  You appear to be creating 24 datasets, for no reason.  You have a call to a macro variable &data_out never defined, and you refer to a variable A in your proc sql which is not selected, and the same across all datasets.

This can all be done in one dataset, and have a variable called period which goes from 1-24, this will make your programming far easier in the long run:

data want;
  do period=1 to 24;
    set sas_out.&data_out.;
    if ...;
    output;
  end;
run;
  

However it is impossible to say without seeing some test data (form of a datastep) and what the output should look like - other than one dataset would be better than many.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

The problem is with the %end.  Macro language should always be encased within %macro and %mend statements such as:

%macro tmp();
  %if a=b %then %do;
    ..
  %end;
%mend tmp;

Secondly, why do you have a load of %let statements in a datastep?  Macro is nothing to do with Base SAS - which is the lanaguage.  Put your %let statements at the top of the code, they are only there for find/replace.  

Your second problem is this statments:

%let %do i=0 %to 24;

Macro do loops are not define with %let.

 

 

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
  • 7 replies
  • 984 views
  • 1 like
  • 6 in conversation