DATA Step, Macro, Functions and more

Is it possible to pass an array as a parameter to a macro?

Reply
Contributor
Posts: 44

Is it possible to pass an array as a parameter to a macro?

I have a call to a macro, as follows:

%GetFundData(2,mfFunds1,mfFunds2,mfFunds3,mfFunds4,mfFunds5,mfFunds6,mfFunds7,mfFunds8,mfFunds9,mfFunds10,mfFunds11,mfFunds12,mfFunds13,mfFunds14,
VDate1,VDate2,VDate3,VDate4,VDate5,VDate6,VDate7,VDate8,VDate9,VDate10,VDate11,VDate12,VDate13,VDate14);

The macro and the call both work as expected. However, is it possible to phrase the call more concisely, e.g.

%GetFundData(2,mfFunds1-mfFunds14,VDate1-VDate14);

having first redefined the macro as follows:

%Macro GetFundDat(2,mfFunds1-mfFunds14,VDate1-VDate14);
...code...
%Mend

I've tried this, but it gives me an error.

I suppose the question is, is it possible to pass array parameters to a macro?

Regular Contributor
Posts: 195

Re: Is it possible to pass an array as a parameter to a macro?

Posted in reply to mediaeval

Try the following code...I have not tested the following code but should meet your requirement...

%Macro invoke;

   

          %GetFundData (2,%do i = 1 %to 13;

                                          mfFunds&i,VDate&i.,

                                  %end;

                                         mfFunds14,VDates14);

%mend;

%invoke;

You can create the macro variables in your macro definition itself rather than reference them as parameter...

-Urvish

Contributor
Posts: 44

Re: Is it possible to pass an array as a parameter to a macro?

Posted in reply to UrvishShah


When I run the code, I get the error,

 

618 %Macro GetFundData(x, %Do i = 1 %To 13; mfFunds&i, VDate&i, %End; mfFunds14,VDate14);

ERROR: Open code statement recursion detected.

ERROR: Expecting comma (to separate macro parameters) or close parenthesis (to end parameter list) but found: &

ERROR: A dummy macro will be compiled.

619 %Local i;

620 %Do i = 14 %To &x+1 %By -1;

621 %End;

622 %Do i = &x %To 1 %By -1;

623 %End;

624 %Mend GetFundData;

Regular Contributor
Posts: 227

Re: Is it possible to pass an array as a parameter to a macro?

Posted in reply to mediaeval

This will not work:

%Macro GetFundDat(2,mfFunds1-mfFunds14,VDate1-VDate14);
because each parameter must conform to SAS naming conventions.

%Macro GetFundDat

(A

,mfFunds

,VDates

);

then you can use this call:

%GetFundDat(2,mfFunds1-mfFunds14,VDate1-VDate14);

Ron Fehd  parameters maven

Respected Advisor
Posts: 3,799

Re: Is it possible to pass an array as a parameter to a macro?

Posted in reply to mediaeval

var1-var10 is not an array but a SAS Variable List called an enumerated variables list.  There should be nothing wrong with using a SAS Variable List but your macro may have to be modified to process it correctly.  Since you say "it gives and error" I would suspect you need to direct you attention to the parts between %MACRO and %MEND.

Contributor
Posts: 44

Re: Is it possible to pass an array as a parameter to a macro?

Posted in reply to data_null__


When I submit the macro code below, I get the following error. It doesn't seem to matter what is in the macro code itself...?

 

611 %Macro GetFundData(x, mfFunds1-mfFunds14,VDate1-VDate14);

ERROR: Expecting comma (to separate macro parameters) or close parenthesis (to end parameter list) but found: -

ERROR: A dummy macro will be compiled.

612 %Local i;

613 %Do i = 14 %To &x+1 %By -1;

614 %End;

615 %Do i = &x %To 1 %By -1;

616 %End;

617 %Mend GetFundData;

Super User
Super User
Posts: 7,060

Re: Is it possible to pass an array as a parameter to a macro?

Posted in reply to mediaeval

You have posted the DEFINITION of the macro.  In that you have have to use actual NAMEs for the macro parameters that you want users to supply values for.  If you used named parameters you can supply default VALUES that can include hyphens.

%Macro GetFundData(dataset=x, funds=mfFunds1-mfFunds14,dates=VDate1-VDate14);


proc print data=&dataset;

   var &funds &dates ;

run;


%mend getfunddata ;


Regular Contributor
Posts: 217

Re: Is it possible to pass an array as a parameter to a macro?

Posted in reply to mediaeval

Try this

/* SYSPBUFF: Automatic Macro Variable that Contains text supplied as macro parameter values.*/

%Macro GetFundData/parmbuff;

    %put Syspbuff contains: &syspbuff;

    %local l_pCount /* Parameter Count */

               l_pi        /* Parameter Index */

               l_pName; /* Parameter Name */

    %let l_pCount=%eval(%sysfunc(countc(%superq(syspbuff),%str(,)))+1);

    %put l_pCount=&l_pCount;

    %do l_pi=1 %to &l_pCount;

        %let l_pName=%scan(%superq(syspbuff),&l_pi,%str(%(,%)));

        %put l_pName=&l_pName;

         /* You can insert additional code based on the current parameter */

    %end;

%Mend GetFundData;

%GetFundData(x,mfFunds1-mfFunds14,VDate1-VDate14);

%GetFundData(x,mfFunds1-mfFunds14);

%GetFundData(mfFunds1-mfFunds14);

Ask a Question
Discussion stats
  • 7 replies
  • 681 views
  • 1 like
  • 6 in conversation