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

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
SASKiwi
PROC Star

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

4 REPLIES 4
SASKiwi
PROC Star

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. 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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?  

Mart_Schm
Calcite | Level 5

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

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.

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
  • 4 replies
  • 893 views
  • 1 like
  • 3 in conversation