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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

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

View solution in original post

6 REPLIES 6
Reeza
Super User

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

dhana
Fluorite | Level 6

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.

Kurt_Bremser
Super User

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)

Ksharp
Super User

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

stat_sas
Ammonite | Level 13

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;

dhana
Fluorite | Level 6

Thank you Ksharp, stat@sas, Reeza and KurtBremser for your replies and insight on how this implicit looping works.

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!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1572 views
  • 3 likes
  • 5 in conversation