Do Loop in a Macro

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

Do Loop in a Macro

Trying to learn using do loop for the first time and this simple one does not work

 

%macro CPCI (r, n, a, b);
%do i = &a %to &b;
Agree = (&r+&i) /&n;
output;
run;
%end;
%mend CPCI;


Accepted Solutions
Solution
3 weeks ago
Super User
Super User
Posts: 9,855

Re: Do Loop in a Macro

Right, Macro language does nothing.  All it does is create some text.  So if you were to call your macro with (5,6,1,2) you would get a text string:

Agree = (5+1) /6;
output;
run;
Agree = (5+2) /6;
output;
run;

That is effectively what that macro does is produce that bit of text based on the parameters I gave. As you can see this is not valid Base SAS - which is the the programming language - as there is no data statement, there are two run's.  So start by writing Base SAS code to do what you want, then, and only then start to change it to macro.  As I can't see what you intend, nor your data, at a guess:

%macro cpci (r, n, a, b);
  %do i=&a. %to &b.;
    agree = (&r.+&i.) /&n.;
    output;
  %end;
%mend cpci;

data want;
  %cpci (5,6,1,2);
run;

Note how I specifically put the decimal place after the macro variable, always do this, although you will get away with it most of the time, when you don't it will really stump you.

View solution in original post


All Replies
Solution
3 weeks ago
Super User
Super User
Posts: 9,855

Re: Do Loop in a Macro

Right, Macro language does nothing.  All it does is create some text.  So if you were to call your macro with (5,6,1,2) you would get a text string:

Agree = (5+1) /6;
output;
run;
Agree = (5+2) /6;
output;
run;

That is effectively what that macro does is produce that bit of text based on the parameters I gave. As you can see this is not valid Base SAS - which is the the programming language - as there is no data statement, there are two run's.  So start by writing Base SAS code to do what you want, then, and only then start to change it to macro.  As I can't see what you intend, nor your data, at a guess:

%macro cpci (r, n, a, b);
  %do i=&a. %to &b.;
    agree = (&r.+&i.) /&n.;
    output;
  %end;
%mend cpci;

data want;
  %cpci (5,6,1,2);
run;

Note how I specifically put the decimal place after the macro variable, always do this, although you will get away with it most of the time, when you don't it will really stump you.

Occasional Contributor
Posts: 5

Re: Do Loop in a Macro

Thank you for this ultra clear explanation!

 

This is indeed my intention since I need to input various value of r, n, a and b.

 

 

Super User
Super User
Posts: 9,855

Re: Do Loop in a Macro

Not entirely sure where you are going with: "input various value of r, n, a and b.", but in either way as @PaigeMiller has stated, datastep loop and in fact all programming is better done in Base SAS, and only use Macro where it really adds value for the amount of effort it is.

Respected Advisor
Posts: 3,288

Re: Do Loop in a Macro

Seems to me you need a data step DO loop here and NOT a macro do loop, with the macro variables used to set the start and finish numbers of the DO loop. I don't see the need for a macro do loop here. But of course, as already pointed out, you need a DATA statement to make this a data step.

--
Paige Miller
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 104 views
  • 2 likes
  • 3 in conversation