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;
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.
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.
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.
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.
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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.