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

Hello,

I am trying to call to a macro within a loop in my data step, and I know call execute can be used for this, but I can't seem to get it to work properly, I keep getting strange errors that the number of parameters don't match (when they do), and the documentation on call execute with a parameterized macro isn't very helpful.

I.e. in the SAS documentation page for call execute the example for this scenario is:

%macro rept(dat,a,dsn);

     proc chart data=&dsn;

          title "Chart for &dat"; where(date="&dat");

          vbar &a; run;

%mend rept;

data _null_;

     set dates;

     call execute('%rept('||date||','||'var1,reptdata)');

run;

I don't understand the use of '||' characters here, why do they surround the first parameter, and not the second & third? Why is the comma between quotes after the first parameter and not the second and third?

My macro definition I am trying to call is as such:

%MACRO Create_Fundamental_Datasets(exp_file=, short_rate=, dep_rate=, shock_scenario=);

My data step code this is attempting call execute is as follows:

data outdata;

  set mydata;

       array shocks (7) short_rate_Flat short_rate_Shock_100 short_rate_Shock_200

       short_rate_Shock_neg100 short_rate_Grad_neg100

       short_rate_Grad_100 short_rate_Grad_200;

       array short_rates (3) Short_Rate_1-Short_Rate_3;

       array deposit_rates(33)Deposit_Rate_1-Deposit_Rate_33;

       do i = 1 to 1;

            do j = 1 to 1;    

                 do k = 1 to 1;  

                    myfile = catt("C:\Documents and Settings\ETRORMC\MyDocuments\SAS_Test_Output\model_5_forecasts_",short_rates,"_vs_", deposit_rates,".xls");    

                      call execute('%Create_Fundamental_Datasets('||short_rates||','||deposit_rates||','||shocks||','||myfile||')');

            end;

       end;

  end;

run;

Clearly I am doing something wrong with the call execute routine. If someone could please explain what I am doing wrong or perhaps just help me better understand how the call execute routine on a macro with multiple parameters is properly constructed I would appreciate it so much.


Thanks a lot!

-Ryan


1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

The error is telling you that you have generated an extra comma some where.

You could generate the text into a variable and put it to the log to help with debugging. Normally I would suspect some character variable such as the file name to be the source of the problem.  Also if you break your long command up into multiple lines it will be easier to debug.  Doing that shows that the extra comma is right at the end before the closing parenthesis for the macro call.

str='%Create_Fundamental_Datasets(exp_file=' || myfile

||',short_rate='     || short_rates

||',dep_rate='       || deposit_rates

||',shock_scenario=' || shocks

||',)'

;

putlog str= ;

call execute(str);

Personally I find it much easier to generate the macro calls to a text file and then %include the file than to use call execute.

Much easier to debug and you can take advantage of the flexibility of the PUT statement.  Especially if you name your dataset variables and your parameters with the same names. It also eliminates some timing issue for when the macro executes and when the code that the macro generates executes.

Example of generating code to text file.

filename code temp;

data _null_;

  set parameters;

  file code;

  put '%Create_Fundamental_Datasets'

      '(' exp_file= $quote.

      ',' short_rate=

      ',' dep_rate=

      ',' shock_scenario=

      ');'

  ;

run;

%inc code / source2;


View solution in original post

6 REPLIES 6
Tom
Super User Tom
Super User

You have defined you macro with named parameters and are generating the macro call using positional parameters. You cannot do that, but you can do the reverse.  If you can modify the macro definition to remove the equal signs after the parameter names then your existing call execute might work. Otherwise add in the parameter names in generated macro call.

NOTE:  || is the concatenation operator.  You could also look into use the CATS or CATX functions.

AllSoEasy
Obsidian | Level 7

Ahh ok that makes things much clearer, however, I am still getting an error after making the suggested fixes. My call execute statement is now as follows:

call execute('%Create_Fundamental_Datasets(exp_file='||myfile||',short_rate='||short_rates||',dep_rate='||deposit_rates||',shock_scenario='||shocks||',)');

-Adding parameter names as specified, my macro definition remains the same, however I still obtain the error:

"ERROR: More positional parameters found than defined."

I don't understand, there is an equal number of parameters...4 in the call and 4 in the definition...what is happening here?? I am at a loss.

Thank you!

-Ryan

Tom
Super User Tom
Super User

The error is telling you that you have generated an extra comma some where.

You could generate the text into a variable and put it to the log to help with debugging. Normally I would suspect some character variable such as the file name to be the source of the problem.  Also if you break your long command up into multiple lines it will be easier to debug.  Doing that shows that the extra comma is right at the end before the closing parenthesis for the macro call.

str='%Create_Fundamental_Datasets(exp_file=' || myfile

||',short_rate='     || short_rates

||',dep_rate='       || deposit_rates

||',shock_scenario=' || shocks

||',)'

;

putlog str= ;

call execute(str);

Personally I find it much easier to generate the macro calls to a text file and then %include the file than to use call execute.

Much easier to debug and you can take advantage of the flexibility of the PUT statement.  Especially if you name your dataset variables and your parameters with the same names. It also eliminates some timing issue for when the macro executes and when the code that the macro generates executes.

Example of generating code to text file.

filename code temp;

data _null_;

  set parameters;

  file code;

  put '%Create_Fundamental_Datasets'

      '(' exp_file= $quote.

      ',' short_rate=

      ',' dep_rate=

      ',' shock_scenario=

      ');'

  ;

run;

%inc code / source2;


SASKiwi
PROC Star

Redundant comma on end of parameter string marked with ^ :

call execute('%Create_Fundamental_Datasets(exp_file='||myfile||',short_rate='||short_rates||',dep_rate='||deposit_rates||',shock_scenario='||shocks||',)');

                                                                                                                                                                                                                           ^

Ron_MacroMaven
Lapis Lazuli | Level 10

Fehd and Carpenter provide comparisons of the answers given here

http://www.sascommunity.org/wiki/List_Processing_Basics_Creating_and_Using_Lists_of_Macro_Variables

while your macro is generated by looping through arrays

in the future you may find this function helpful: it reads a data set and generates calls to macros

with each variable as a named parameter

http://www.sascommunity.org/wiki/Macro_CallMacr

Ron Fehd  the other guy

AllSoEasy
Obsidian | Level 7

Ahh it was a silly mistake on my part. The problem has been rectified, thank you all for your time and help!

-Ryan

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 6 replies
  • 2122 views
  • 7 likes
  • 4 in conversation