BookmarkSubscribeRSS Feed
ejohnson96
Calcite | Level 5

is there a way to call an already created macro function and use it in another macro function that I am trying to create?

3 REPLIES 3
Reeza
Super User

For functions you want Proc FCMP. 

Macros don't return values. 

 

You can call a macro within any other macro once it's been compiled. 

 

%macro_name(parameters);

 

If your replicating R code you can consider SAS/IML instead of Base. 

Otherwise you may want to define your functionality and figure out the best method in SAS. Trying to replicate exactly is more work in long run. My nickel. 

LinusH
Tourmaline | Level 20
Technically macros generate text (preferably SAS code).
But, you could design a macro so it behaves like a function, like returning a value in an assignment statement.
Depending on the nature of your macro, PROC FCMP might be a better choice as @Reeza suggest.
Bottom line, yes you can call a macro from another macro.
What does your "macro functions" do?
Data never sleeps
jklaverstijn
Rhodochrosite | Level 12

Yes you can.

 

In the same session you can do stuff like this:

 

%macro outer;
   %put In outer;
    %inner
%mend

%macro inner;
    %put In inner
%mend;

%outer;

The inner macro needs to be defined at call time, not nescecarily when %Outer is defined. And %inner can be redefined without the need for %outer to be recompiled.

 

Existing macros can also be defined earlier or outside SAS. Store them in a file with the .sas extension and have the SASAUTOS option point to its location.

 

As @Reeza pointed out, macro's do not technically return a value like a function does. But it is conceptually close enough. Just look at the %SYSRC autocall macro for a nice example.

 

Hope this helps,

- Jan.

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
  • 3 replies
  • 1126 views
  • 0 likes
  • 4 in conversation