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
%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.
You are trying to use macro language without knowing basic concepts:
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.
Hi ,
What should the output dataset test look like ?
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?
%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.
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
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.
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.