BookmarkSubscribeRSS Feed
mediaeval
Calcite | Level 5

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?

7 REPLIES 7
UrvishShah
Fluorite | Level 6

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

mediaeval
Calcite | Level 5


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;

Ron_MacroMaven
Lapis Lazuli | Level 10

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

data_null__
Jade | Level 19

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.

mediaeval
Calcite | Level 5


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;

Tom
Super User Tom
Super User

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 ;


AhmedAl_Attar
Ammonite | Level 13

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

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 7 replies
  • 3322 views
  • 2 likes
  • 6 in conversation