BookmarkSubscribeRSS Feed
BrahmanandaRao
Lapis Lazuli | Level 10
%macro xyz;
%do i=1 %to 20 ;
if mod(%eval(&i,2))=0 %then %do;
%end;
%end;
%mend;

%xyz;

error

7 REPLIES 7
ballardw
Super User

Do what?

Your "if" is wrong to execute in macro processing, it should be %if.

To call data step functions you have to use %sysfunc(function(parameters))

 

%macro xyz;
   %do i=1 %to 20 ;
      %if %sysfunc (mod(&i,2))=0 %then %do;
      %end;
   %end;
%mend;

 

 

BrahmanandaRao
Lapis Lazuli | Level 10
%macro xyz;
   %do i=1 %to 20 ;
   data even;
      %if %sysfunc (mod(&i,2))=0 %then %do;
      %end;
      run;
   %end;
%mend;

%xyz;
Astounding
PROC Star
Not fair. You can't change the problem by adding a DATA statement and a RUN statement. The answer would change as well. Try showing the DATA step code and the results that you hope the program will generate. You may not need any macro language at all.
ballardw
Super User

@BrahmanandaRao wrote:
%macro xyz;
   %do i=1 %to 20 ;
   data even;
      %if %sysfunc (mod(&i,2))=0 %then %do;
      %end;
      run;
   %end;
%mend;

%xyz;

NOW you really need to describe everything that you are actually attempting to do. %if  code does not recognize the values of data step variables. Macro statements inside data step must generate valid data step (or other procedure) code. Since the code you show does not generate data step code it does nothing except create a data set named even, with no variables and gets overwritten 19 times after initial creation.

 

At this point I would say that one possible "solution" is : 42 .

 

Astounding
PROC Star
To macro language, mod is not a function. It is just the three letters: mod. To get macro language to treat mod as a function (and to clean up a couple of smaller errors along the way), try this untested version:

%if %sysfunc(mod(&i,2))=0 %then %do;

In fact, in this particular case, you could instead get rid of the mod function if you so choose. Since macro language drops the remainder when it applies %eval, you could try:

%if &i = &i/ 2 * 2 %then %do;
PaigeMiller
Diamond | Level 26

As always, @BrahmanandaRao we need much more explanation from you. Your posts are consistently too brief, causing us to guess and often guess wrong and then we have to ask follow-up question after follow-up question. Explain. Provide details about what you are trying to do, in words, not SAS code, and explaining the background and reasons. Emphasis on details. Emphasis on background. Emphasis on explain. Don't make us constantly ask you for additional details and ask why you are doing this; provide all relevant details in the first post. We're trying to help you, but you have to help us.

--
Paige Miller
Kurt_Bremser
Super User

Your code (once corrected to use %IF and %SYSFUNC) will do nothing, so it's not worth bothering with.

Rule #1 of macro development: start with working, non macro code. So please show the code you want to make dynamic, and describe what needs to be repeated, and how.

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 2010 views
  • 2 likes
  • 5 in conversation