Hello,
is it possible in SAS to call a macro and write the macro at the end of the programm?
It works, but i would need to run the programm twice. Are there any other ideas?
For example:
%mymacro;
proc sql;
select id_discriminator, count(*)
from work.macro_table
group by 1;
quit;
%macro mymacro;
proc sql;
create table work.macro_table as
select id_discriminator
from data_have;
quit;
%mend;
Thanks in advance,
Martin
A macro has to be defined before it is called. One option is to put your macro into an AUTOCALL macro library from where it is defined automatically when referenced in your program.
A macro has to be defined before it is called. One option is to put your macro into an AUTOCALL macro library from where it is defined automatically when referenced in your program.
You can put a macro definition anywhere, so long as it appears *before* the call to that macro. Autocall library is one option. Saving to a text file and using %include at the top of your program is another. What exactly are you attemting to do?
my goal was (is) to make the code more readable.
I read now about autocall librarys and i think this works fine for me.
Thanks for your answer
Easiest ways to make code more readable other than using good coding practices (such as consistent casing, indentation, finishing blocks etc.) would be to reduce the use of macro language. Macro language is an additional tool which is only there to remove the need to repeat code, however it is vastly abused. There should almost never be a time where you resort to macro as you can't do something in Base SAS. Also, don't wrap self explanatory code in macros, too often I see:
%macro dosomething ();
proc sort data=abc;data abc; set x=1;
%mend;
This code is a total waste of space, and obfuscates it totally to no additional value.
The reason I mention this is because, from a readability sense, so long as a macro has followed SDLC processes, you will have a function design specification, along with probably a user guide. So seeing the code should not really be necessary because of the good documentation. However in 99% of the macro libraries I come across there is zero documentation, poor programming standards, and plenty of obfuscation.
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!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.