BookmarkSubscribeRSS Feed
Anandkvn
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;

 

 

Anandkvn
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
Opal | Level 21
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

@Anandkvn 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
Opal | Level 21
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, @Anandkvn 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 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

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