All -
I knew that we can't perform arithmetic operation directly on the macro variables, if we have to perform the same then we need to use either %EVAL or %SYSEVALF macro functions.
%LET A=3;
%LET B=4;
%LET SUM=&A+&B;
%PUT SUM=&SUM.;
%LET SUM=%EVAL(&A+&B);
%PUT SUM=&SUM.;
But if you look at the below code, the do loop iterating 10 times through the %put statement without even incrementing the counter i by 1. Can anyone explain how this is happening.
%MACRO LOOPCHK;
%DO I=1 %TO 10;
%PUT I=&I;
%END;
%MEND LOOPCHK;
%LOOPCHK;
By Default, SAS will take %DO I=1 %TO 10; I as Integer just like %DO %eval( I=1 ) %TO %eval(10) ; You can find it in Macro Documentation. if I is double or float , this wouldn't work . you need %sysevalf() around it.
Xia Keshan
You have something else wrong somewhere, all of your code above works as expected..
1 %MACRO LOOPCHK;
2
3 %DO I=1 %TO 10;
4 %PUT I=&I;
5 %END;
6
7 %MEND LOOPCHK;
8
9 %LOOPCHK;
I=1
I=2
I=3
I=4
I=5
I=6
I=7
I=8
I=9
I=10
10 %LET A=3;
11 %LET B=4;
12 %LET SUM=&A+&B;
13 %PUT SUM=&SUM.;
SUM=3+4
14
15 %LET SUM=%EVAL(&A+&B);
16 %PUT SUM=&SUM.;
SUM=7
Reeza,
I don't have problem with my code. My question is when we need macro functions like evalf & sysevalf to perform arithmetic operation, how the same is performed without any macro functions in %do loop macro statement.
That is just a function of the iterative %do. It does the increment implicitly.
You probably miss the constructs you would expect in a C language "for" statement. (initialize, increment, end condition)
By Default, SAS will take %DO I=1 %TO 10; I as Integer just like %DO %eval( I=1 ) %TO %eval(10) ; You can find it in Macro Documentation. if I is double or float , this wouldn't work . you need %sysevalf() around it.
Xia Keshan
Hi,
Try this, may help in understanding the concept.
Thanks,
Naeem
options symbolgen;
%MACRO LOOPCHK;
%DO I=1 %TO 10;
%PUT I=&I;
%END;
%MEND LOOPCHK;
%LOOPCHK;
options symbolgen;
%MACRO LOOPCHK;
%let I=1;
%DO %while (&I<=10);
%PUT I=&I;
%let I=%eval(&I+1);
%END;
%MEND LOOPCHK;
%LOOPCHK;
Thank you Ksharp, stat@sas, Reeza and KurtBremser for your replies and insight on how this implicit looping works.
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.