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

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);

1 ACCEPTED SOLUTION

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

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

11 REPLIES 11
user24feb
Barite | Level 11

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);

alan0101
Obsidian | Level 7

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

alan0101
Obsidian | Level 7

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);

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

alan0101
Obsidian | Level 7

Thanks RW9, much appreciated

alan0101
Obsidian | Level 7

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 ?

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

alan0101
Obsidian | Level 7

Thanks again - perfect !

LinusH
Tourmaline | Level 20

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
Ksharp
Super User

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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