Macro Parameters

Accepted Solution Solved
Reply
Contributor
Posts: 40
Accepted Solution

Macro Parameters

Hi

Is it possible to specify multiple values for one macro parameter, for example :


%macro Links (ClientId=);

     code

     code

%mend Links;

%Links (ClientId=1,2,3,4);


Accepted Solutions
Solution
‎08-14-2014 04:56 AM
Super User
Super User
Posts: 7,401

Re: Macro Parameters

So yes:

%macro Links (ClientId=);

     code;

%mend Links;

data have;

     do client=1 to 4;

          output;

     end;

run;

data _null_;

     set have;

     call execute('%Links (ClientId='||strip(put(client,best.))||');');

run;

View solution in original post


All Replies
Super Contributor
Posts: 336

Re: Macro Parameters

Sure, you can, but I would not use a comma as a separating character:

%Macro Links(ClientID=);

/* maybe work on a list .. */

%Let i=1;

%Let ID=%Scan(&ClientID.,1,' ');

%Do %While (&ID^=);

  %Put **&ID.**;

  %Let i=%Eval(&ID.+1);

  %Let ID=%Scan(&ClientID.,&i.,' ');

%End;

%Mend;

%Links(ClientID=1 2 3 4);

Contributor
Posts: 40

Re: Macro Parameters

Hmmm, I think I could be missing something here.  I am trying to get the macro to iterate once for every value of ClientId, but I am not sure that this is what it was intended for.

Super User
Super User
Posts: 7,401

Re: Macro Parameters

Hi,

So what is it your trying to achieve?  You can pass a delimited list of data in as User24feb has mentioned.  There is also another syntax for parmbuff which you can pass any number of parameters in.  You could also generate the code which is my preference.

data have;

     client="ABC"; output;

     client="BBD"; output;

run;

data _null_;

     set have;

     call execute('%Links('||strip(client)||');');   /* Creates a macro call for each row of dataset */

run;

Without knowing exactly what you want to achieve though its hard to provide concrete examples.

Contributor
Posts: 40

Re: Macro Parameters

Basically, I want the macro to iterate for each value of ClientId, something like this (assume there are only 4 ClientID's : 1,2,3,4) :

%macro Links (ClientId=);

     code

     code

%mend Links;

%Links (ClientId=1);


%macro Links (ClientId=);

     code

     code

%mend Links;

%Links (ClientId=2);


%macro Links (ClientId=);

     code

     code

%mend Links;

%Links (ClientId=3);


%macro Links (ClientId=);

     code

     code

%mend Links;

%Links (ClientId=4);

Solution
‎08-14-2014 04:56 AM
Super User
Super User
Posts: 7,401

Re: Macro Parameters

So yes:

%macro Links (ClientId=);

     code;

%mend Links;

data have;

     do client=1 to 4;

          output;

     end;

run;

data _null_;

     set have;

     call execute('%Links (ClientId='||strip(put(client,best.))||');');

run;

Contributor
Posts: 40

Re: Macro Parameters

Thanks RW9, much appreciated

Contributor
Posts: 40

Re: Macro Parameters

Oops, one last thing RW9. Our ClientID's are not in numerical order, so would, for example, be 1,5,7,9.  How would I phrase the do loop (where you say do client=1 to 4) to accommodate this ?

Super User
Super User
Posts: 7,401

Re: Macro Parameters

I will also compress the code a bit now you have seen it:

%macro Links (ClientId=);

     code;

%mend Links;

data have;

     do client=1,5,7,9;

          call execute('%Links (ClientId='||strip(put(client,best.))||');');;

     end;

run;

Contributor
Posts: 40

Re: Macro Parameters

Thanks again - perfect !

Super User
Posts: 5,257

Re: Macro Parameters

Yes you could, but then you need to parse the parameter and build a %do loop inside the macro.

If you wish to execute the whole macro for each value, you might want to look at calling the macro multiple times instead. If you have many values, you could drive this by storing the values in a data set, and call the macro dynamically by call execute.

Data never sleeps
Super User
Posts: 9,681

Re: Macro Parameters

Use minoperator

%macro Links (ClientId=) / minoperator   ;

%if &ClientId  in  1 2 3 4  %then %do ;

     code

     code

%mend Links;

%Links (ClientId=1 )

%Links (ClientId=2 )

%Links (ClientId=12 )

Xia Keshan

☑ This topic is SOLVED.

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

Discussion stats
  • 11 replies
  • 498 views
  • 0 likes
  • 5 in conversation