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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Satish_Parida
Lapis Lazuli | Level 10

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.

View solution in original post

2 REPLIES 2
Astounding
PROC Star
The answer is more complex than you are ready to digest at this point. Add this statement first:

Options mprint symbolgen;

Then rerun the program and read the log.
Satish_Parida
Lapis Lazuli | Level 10

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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 2 replies
  • 575 views
  • 0 likes
  • 3 in conversation