DATA Step, Macro, Functions and more

How to use loops for macro function calls in SAS

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

How to use loops for macro function calls in SAS

I am using the following statements for function calls in sas

 

 

%loopit(26062015,bbbbbbINFY,FUTSTK,30Jul2015);
%loopit(26062015,bbbbbbSBIN,FUTSTK,30Jul2015);
%loopit(26062015,bbbbbbbTCS,FUTSTK,30Jul2015);


where i have already have defined the loopit macro previously in the code. As it can be seen, in the function calls, only one parameter changes while the rest are same. I wanted to if there are any loop structures (e.g. Arrays) I can use to make it more useful.

 


Accepted Solutions
Solution
‎09-23-2016 02:26 AM
Super User
Posts: 7,762

Re: How to use loops for macro function calls in SAS

Put the control values into a dataset, or do the call execute from a cards; block:

data _null_;
input loop_val $10.;
call execute('%loopit(26062015,'!!loop_val!!',FUTSTK,30Jul2015);');
cards;
bbbbbbINFY
bbbbbbSBIN
bbbbbbbTCS
;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Super User
Posts: 19,771

Re: How to use loops for macro function calls in SAS

If your parameters are in a data set look into CALL EXECUTE to call your macro.  

Solution
‎09-23-2016 02:26 AM
Super User
Posts: 7,762

Re: How to use loops for macro function calls in SAS

Put the control values into a dataset, or do the call execute from a cards; block:

data _null_;
input loop_val $10.;
call execute('%loopit(26062015,'!!loop_val!!',FUTSTK,30Jul2015);');
cards;
bbbbbbINFY
bbbbbbSBIN
bbbbbbbTCS
;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
New Contributor
Posts: 4

Re: How to use loops for macro function calls in SAS

Posted in reply to KurtBremser

Thanks for your solution. It really helped. Now I have created a Dataset with the list of the scrips and using call execute to call the macro as suggested.

Super User
Super User
Posts: 7,942

Re: How to use loops for macro function calls in SAS

Why are you doing that in the first place?  Datasteps are a loop, and have all the data structures and syntax necessary to perform such tasks.  So simplfy your code to:

data want;
  set have;
  do i="bbbbbINFY","bbbbbSBIN","bbbbbTCS";
    /* some code */
  end;
run;

Macro language is not a replacement for Base SAS.

Super Contributor
Posts: 340

Re: How to use loops for macro function calls in SAS

%Let Macro_List=bbbbbbINFY bbbbbbSBIN bbbbbbbTCS;
%Macro Loop;
%Do i=1 %To %Sysfunc(CountW(&Macro_List.));
  %Let Param=%Scan(&Macro_List.,&i.);
  %loopit(26062015,&Param.,FUTSTK,30Jul2015);
%End;
%Mend;
%Loop
Super User
Posts: 11,343

Re: How to use loops for macro function calls in SAS

If your macro is mostly going to be used with the same values you can use keyword parameters to set a default value to reduce how much is entered.

Obviously I do not know your macro variable names but this should give you an idea;

 

%macro %loopit(D1 =26062015, str=, Var2=FUTSTK, Date=30Jul2015)

Then the call would look like

%loopit(Str=bbbbbbINFY);

The values for the other parameters would be as defined in the macro.

If you need to use a different value for one of those parameters such as date:

%loopit(Str=bbbbbbINFY, Date= 08Aug2016);

 

☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 466 views
  • 4 likes
  • 6 in conversation