Hi everyone! I am learning to use macro in SAS and I have got a question.
In my understanding, we have to use %DO to let SAS to know we are using do statement. However, I found that the script is still working when do (without % sign) is used in nested do loop. Is that mean % sign is not necessary to be used for nested do loop in macro? Many thanks.
Sample Script:
%macro mymacro;
%let i=0;
%let j=0;
%do %until(&i>5);
do until(&j>5);
%let product=%sysevalf(&i*&j);
%put The result is &product;
%let j=%eval(&j+1);
end;
%let i=%eval(&i+1);
%mend
The Correct code is bellow.
%macro mymacro;
%let i=0;
%let j=0;
%do %until(&i>5);
%do %until(&j>5);
%let product=%sysevalf(&i*&j);
%put The result is &product;
%let j=%eval(&j+1);
%end;
%let i=%eval(&i+1);
%end;
%mend;
%mymacro;The Macro compiler is called by a word processor in SAS.
When you wrote a do in side a %do, SAS assumed that the do is a part of some other sas statement which will be used after the macro processing. That you can see if you try to execute the macro. You will get the following error.
NOTE: Line generated by the invoked macro "MYMACRO".
85 do until(&j>5);
__
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
The result is 0
NOTE: Line generated by the invoked macro "MYMACRO".
85 end;
___
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
NOTE: Line generated by the invoked macro "MYMACRO".
85 do until(&j>5);
__
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
The result is 1
NOTE: Line generated by the invoked macro "MYMACRO".
85 end;
___
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
NOTE: Line generated by the invoked macro "MYMACRO".
85 do until(&j>5);
__
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
The result is 4
.
.
.Macro is a very powerfull lnguage which can be mixed with BASE SAS to get results, but macro can not be mixed with BASE SAS utilities alone.
The Correct code is bellow.
%macro mymacro;
%let i=0;
%let j=0;
%do %until(&i>5);
%do %until(&j>5);
%let product=%sysevalf(&i*&j);
%put The result is &product;
%let j=%eval(&j+1);
%end;
%let i=%eval(&i+1);
%end;
%mend;
%mymacro;The Macro compiler is called by a word processor in SAS.
When you wrote a do in side a %do, SAS assumed that the do is a part of some other sas statement which will be used after the macro processing. That you can see if you try to execute the macro. You will get the following error.
NOTE: Line generated by the invoked macro "MYMACRO".
85 do until(&j>5);
__
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
The result is 0
NOTE: Line generated by the invoked macro "MYMACRO".
85 end;
___
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
NOTE: Line generated by the invoked macro "MYMACRO".
85 do until(&j>5);
__
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
The result is 1
NOTE: Line generated by the invoked macro "MYMACRO".
85 end;
___
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
NOTE: Line generated by the invoked macro "MYMACRO".
85 do until(&j>5);
__
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
The result is 4
.
.
.Macro is a very powerfull lnguage which can be mixed with BASE SAS to get results, but macro can not be mixed with BASE SAS utilities alone.
Nearly 200 sessions are now available on demand with the SAS Innovate Digital Pass.
Explore Now →SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.