Help with Macro running discrete value variable_passing into main code

Accepted Solution Solved
Reply
Super Contributor
Posts: 402
Accepted Solution

Help with Macro running discrete value variable_passing into main code

Hi Everyone,

The macro code below let x run from start to end.

However, I would like x to take a number of value say, 4,6,20...

I dont know how to get it done.

Could you please help me to do it?

Thank you,

HHC


%macro summary(start=, end=);

     %do x=&start %to &end %by 2;

     ...

  %end;

%mend;


Accepted Solutions
Solution
‎07-29-2014 01:50 PM
Occasional Contributor
Posts: 17

Re: Help with Macro running discrete value variable

SAS Macro loop does not support this kind of iterations, there was a discussion on it but I can't seem to find the link. By saying that you can't, you can still cheat it, such as:

%let oldvar=4 6 20;

%macro test;

%do i=1 %to %sysfunc(countw(&oldvar));

%let newvar=%scan(&oldvar,&i);

data test&newvar;

set sashelp.class;

run;

%end;

%mend;

%test;

So you still get to iterate you various values.

Hope this helps,

Haikuo

View solution in original post


All Replies
Solution
‎07-29-2014 01:50 PM
Occasional Contributor
Posts: 17

Re: Help with Macro running discrete value variable

SAS Macro loop does not support this kind of iterations, there was a discussion on it but I can't seem to find the link. By saying that you can't, you can still cheat it, such as:

%let oldvar=4 6 20;

%macro test;

%do i=1 %to %sysfunc(countw(&oldvar));

%let newvar=%scan(&oldvar,&i);

data test&newvar;

set sashelp.class;

run;

%end;

%mend;

%test;

So you still get to iterate you various values.

Hope this helps,

Haikuo

Super Contributor
Posts: 402

Re: Help with Macro running discrete value variable

Thank you, Haikuo.

It works perfectly!

HHC

Super User
Posts: 9,856

Re: Help with Macro running discrete value variable

MINOPERATOR:

%macro test(x=)/minoperator mindelimiter=','  ;
%if &x in 4,6,20 %then %do;
data test&x;
set sashelp.class;
run;
%end;
%mend;
%test(x=4)
%test(x=44)


Xia Keshan

Super User
Super User
Posts: 7,682

Re: Help with Macro running discrete value variable

Or:

data _null_;

     do I=4,6,20;

          call execute('data datafor_'||strip(put(I,best.))||';

                                   set have;

                                   where the_variable='||strip(put(I,best.))||';

                               run;');

     end;               /* You could also have the above call a macro with I as parameter */

run;

🔒 This topic is solved and locked.

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

Discussion stats
  • 4 replies
  • 454 views
  • 3 likes
  • 4 in conversation