DATA Step, Macro, Functions and more

macro with changing argument

Occasional Contributor
Posts: 15

macro with changing argument

Hi everyone, I am more used to matlab data manipulation and find Sas
synthesis difficult to understand sometimes.

Suppose the macro 'prog_iter' is programmed correctly. It takes a value 'k'
as input. My question is how to use do-loop to let k being different values .
(Eg. for k=0.
01 to 1 by step of 0.01) or any ways.

Thank you in advance
Regular Contributor
Posts: 171

Re: macro with changing argument

Valued Guide
Posts: 2,175

Re: macro with changing argument

you may find that you could insert a %DO loop inside the macro where you want to do some looping.
Alternatively you can create a wrapper macro that will derive the value you want to supply and then call your %prog_iter. Something like this untested code:

%macro looping( from_value= 0, to_value= 1, by_val= .01 );
%* but because simple old sas macro iterative %do from/to/by looping expects integers, ...;
%let this val = &from_val ;
%do %while( %sysevalf( &this_val LE &to_val ) ;
%prog_iter( k= &this_val )
%let this_val = %sysevalf( &this_val + &by_val ) ;
%end ;
%mend looping ;
which you could invoke with
%looping( from_value= .01, to_value= .9, by_val= .015 )

but be very careful before extending the use of this %looping to negative &by_val ;-)

Posts: 8,743

Re: macro with changing argument

This is a good paper that explains the basics of macro processing. It covers what a macro program is and why you can only use %IF and %DO in a macro program (Step 9).

Of course, you'll need to read the rest of the steps and understand them before you proceed.

Super User
Posts: 9,681

Re: macro with changing argument

I understand what polingjw want to point out. Smiley Happy


%macro prog_iter(k=);
%put k=&k;
%mend prog_iter;

Data _null_;
do i=0.1 to 1 by 0.1;
call execute('%prog_iter(k='||i||')');

Occasional Contributor
Posts: 15

Re: macro with changing argument

The call execute method is clean and clear. Macro wrapper method is instructive too. Before that, I was even thinking how to let the %pro_iterate(k=) to accept a sas data set as input, which has values 0.01, 0.02,... 0.10 !

Thank all of you.
Ask a Question
Discussion stats
  • 5 replies
  • 5 in conversation