Desktop productivity for business analysts and programmers

Write Macro at end of Code

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Write Macro at end of Code

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

 


Accepted Solutions
Solution
‎12-09-2016 03:59 AM
Super User
Posts: 3,233

Re: Write Macro at end of Code

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. 

View solution in original post


All Replies
Solution
‎12-09-2016 03:59 AM
Super User
Posts: 3,233

Re: Write Macro at end of Code

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. 

Super User
Super User
Posts: 7,691

Re: Write Macro at end of Code

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?  

New Contributor
Posts: 2

Re: Write Macro at end of Code

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

Super User
Super User
Posts: 7,691

Re: Write Macro at end of Code

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.

☑ This topic is solved.

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

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