Issue with macro variables %let

Accepted Solution Solved
Reply
Contributor
Posts: 42
Accepted Solution

Issue with macro variables %let

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


Accepted Solutions
Solution
‎05-05-2017 10:31 AM
Super User
Posts: 7,444

Re: Issue with macro variables %let

%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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Super User
Posts: 5,367

Re: Issue with macro variables %let

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.

New Contributor
Posts: 3

Re: Issue with macro variables %let

Hi ,

 

What should the output dataset test look like ?

PROC Star
Posts: 276

Re: Issue with macro variables %let

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?

Solution
‎05-05-2017 10:31 AM
Super User
Posts: 7,444

Re: Issue with macro variables %let

%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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 42

Re: Issue with macro variables %let

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

Super User
Super User
Posts: 7,720

Re: Issue with macro variables %let

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.

Super User
Super User
Posts: 7,720

Re: Issue with macro variables %let

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.

 

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 156 views
  • 1 like
  • 6 in conversation