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

Hello!

 

I'm trying to run my macro (%tti(number)) that performs a large series of operations using an input value of 'number'. I have 2000 such numbers that changes the output of the macro. I have looped though them all using a simple loop macro:

 

 

%macro loop(start,stop);
%do iii=&start %to &stop;
%tti(&iii);
%end;
%mend();

However, now I would like to create the same macro but no do start to stop but rather run the macro based on input numbers, lets says 3,5,7,33,99.

 

I have tried to use code presented on this forum with no success.

 

%do_this(3 5 7 33 99);


%macro do_this(ids);
    %let num_ids=%sysfunc(countw(&ids));
    %do i=1 %to &num_ids;
         %let this_id=%scan(&ids,&i,%str( ));
         %tti(&this_id);
         run;
    %end;
%mend;

This macro runs the %tti() but using wrong input and sometimes gets stuck on the last input variable (in this case 99) and reruns it indefinitely. When inputting only 2 numbers in the list it only performs the 1st.

 

Any suggestions?

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Nothing serious is wrong with your posted code.

Make sure that the TTI macro is not changing the macro variables I or NUM_IDS.

Remember to ALWAYS declare the macro variables that are local to your macro as %LOCAL to avoid this type of issue when nesting macro calls.  So your TTI macro might have a %DO loop that also using I as the index macro variable name. Like:

%macro tti(arg1);
%local i;
....
%do i=
 ...

%mend tti;

And if it does NOT have the %LOCAL statement then the macro variable I that the %DO loop would modify would be the one defined in DO_THIS instead.

 

So your little DO_THIS macro should do the same thing just in case you decide to call it from some other macro.

%macro do_this(ids);
%local num_ids i this_id;
    %let num_ids=%sysfunc(countw(&ids));
    %do i=1 %to &num_ids;
         %let this_id=%scan(&ids,&i,%str( ));
         %tti(&this_id);
    %end;
%mend;

 

View solution in original post

5 REPLIES 5
Tom
Super User Tom
Super User

Nothing serious is wrong with your posted code.

Make sure that the TTI macro is not changing the macro variables I or NUM_IDS.

Remember to ALWAYS declare the macro variables that are local to your macro as %LOCAL to avoid this type of issue when nesting macro calls.  So your TTI macro might have a %DO loop that also using I as the index macro variable name. Like:

%macro tti(arg1);
%local i;
....
%do i=
 ...

%mend tti;

And if it does NOT have the %LOCAL statement then the macro variable I that the %DO loop would modify would be the one defined in DO_THIS instead.

 

So your little DO_THIS macro should do the same thing just in case you decide to call it from some other macro.

%macro do_this(ids);
%local num_ids i this_id;
    %let num_ids=%sysfunc(countw(&ids));
    %do i=1 %to &num_ids;
         %let this_id=%scan(&ids,&i,%str( ));
         %tti(&this_id);
    %end;
%mend;

 

Kurt_Bremser
Super User

Run your macro off a dataset with call execute():

data control;
input i;
datalines;
3
5
7
33
99
;
run;

data _null_;
set control;
call execute(cats('%nrstr(%tti(',i,'))'));
run;
Tom
Super User Tom
Super User

The data step DO loop allows you to use a list of values like that. Why not just use a data step to generate the macro calls?

data _null_;
  do id=3,5,7,33,99;
    call execute(cats('%nrstr(%tti)(',id,')'));
  end;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 5 replies
  • 5269 views
  • 3 likes
  • 3 in conversation