Desktop productivity for business analysts and programmers

Call macro from macro using variable value as parm

Accepted Solution Solved
Reply
Contributor
Posts: 24
Accepted Solution

Call macro from macro using variable value as parm

Hope someone can help me.

 

I would like to call a macro from within another macro but only every time 'category' changes, and then pass the initiate macro the value of a variable sorted_profile_table.

 

I have spent hours on this and all the help online isn't helping, thank you for your assistance!  

 

Here is the code (pseudocode in some cases): 

 

%macro load_control_tables;

 

 proc sort data=profile_table out=sorted_profile_table;
  by category;
 run;

 

 data _null_;
  set sorted_profile_table ;
  by category;

 

  if first.category then
  do;  
      call another macro (called %initiate) using the VALUE of a VARIABLE from sorted_profile_table (as a macro variable)
  end;
 run;

%mend;

 

 The macro @initiate is within another program and has been run.

 

Thanks folks!
Mark


Accepted Solutions
Solution
3 weeks ago
PROC Star
Posts: 1,339

Re: Call macro from macro using variable value as parm

[ Edited ]

You need Call execute

 

Please refer to the technical paper by the genius none other than Ian whitlock

http://www2.sas.com/proceedings/sugi22/CODERS/PAPER70.PDF

 

View solution in original post


All Replies
Solution
3 weeks ago
PROC Star
Posts: 1,339

Re: Call macro from macro using variable value as parm

[ Edited ]

You need Call execute

 

Please refer to the technical paper by the genius none other than Ian whitlock

http://www2.sas.com/proceedings/sugi22/CODERS/PAPER70.PDF

 

Contributor
Posts: 24

Re: Call macro from macro using variable value as parm

Posted in reply to novinosrin
Thank you very much novinosrin, worked! Good on you :-)
Contributor
Posts: 24

Re: Call macro from macro using variable value as parm

Posted in reply to novinosrin

Hello there,

 

Subsequent to this, what if the parameters to a macro call are longer than 262 characters, it seems CALL EXECUTE fails in this case?

 

Kind regards,

Mark

Super User
Super User
Posts: 9,211

Re: Call macro from macro using variable value as parm

If your having parameters that long then consider re-working your process.  There is never a need to use macros, just change the way of thinking about things.  One of the main things we see pop up here is people passing in lists of items.  Take this example, you need to filter off a list of items, you could put those items in a macro parameter like:

%macro filter (list=);
  proc sql;
    select * from have where myvar in (&list.);
  quit;
%mend filter;

Now if your list grows this type of thing can get unwieldy and hard to program with.  Use the power of datasets makes this far simpler, just with a simple change of thinking:

%macro filter (listds=);
  proc sql;
    select * from have where myvar in (select var from &listds.);
  quit;
%mend filter;

In this instance we have a dataset with our values, and pass that into the macro, the dataset can be any size and be used just like any other dataset.   

 

With regards to the "longer than 262 characters, it seems CALL EXECUTE fails in this case?" - what do you mean fails?  What happens, what is in the log etc.  Do you mean you get a warning about length, if so there is: options noquotelenmax; which will turn off that warning, make sure to turn it back on afterwards.

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 146 views
  • 1 like
  • 3 in conversation