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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

CHELS
Obsidian | Level 7

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.

 

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

PaigeMiller
Diamond | Level 26

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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 4 replies
  • 677 views
  • 2 likes
  • 3 in conversation