How do I dynamically create a list of all macro functions created in a program. I know how to obtain a list of reference macros, but not macro functions. How do I do that? The macro functions are temporary - created everytime the program is run. I'm trying to create documentation for code others run since they have don't have any documentation.
"functions"?
If you want to scan code to see if it defines any macros look for the %MACRO statement.
If you want to check after running a program what macros are defined you can look at the WORK.SASMACR (or SASMAC1 if using Enterprise Guide or SAS/Studio or other esoteric way to submit SAS code) and see what macro entries are there. For an example of how to do that look at this macro: https://github.com/sasutils/macros/blob/master/maclist.sas
I like Tom's idea of checking WORK.SASMACR.
You could write a macro, %ReportMacrosUsed which will look in WORK.SASMACR and get a list of all of the macros there, and write that list and the name of the program somewhere useful (a file, database, email, whatever). Then tell all of your users to add a call to %ReportMacrosUsed to the end of their programs.
You could use the %MACLIST() macro to make a VIEW and then just ask them to print the VIEW at the end of their programs.
Make the view into some fixed location. Say your company has a SYSTEM library where you store common data for them to use then your admins could run this %MACLIST() call to make the view.
%maclist(out=system.maclist/view=system.maclist)
Then the users can add this step to the end of their programs:
proc print data=system.maclist;
title "Macros used by this run";
run;
Do you mean all macro definitions in a program?
What you do you mean by reference macros and macro functions?
Since SAS programs are text files, you could use SAS (or something else) to parse a .sas file to look for %macro statements and grab them name of each macro, but it would be a bit of an exercise.
Another option might be to turn on logging options like MCOMPILENOTE which will write to the log when a macro is compiled. Then you could run a program, and save the log, and then parse the log to see which macros were compiled.
But either way seems kind of messy.
1 options MCOMPILENOTE=ALL;
2
3 %macro test1234();
4 xxx
5 %mend;
NOTE: The macro TEST1234 completed compilation without errors.
5 instructions 52 bytes.
you could try like this:
proc sql;
create table Before_processing as
select memname, objname, objtype
from dictionary.catalogs
where
objtype = "MACRO"
and libname = "WORK"
;
quit;
/*
your code goes here
*/
/*For example:*/
/* packages to be loaded at the system start */
filename packages "C:\SAS_WORK\SAS_PACKAGES";
%include packages(SPFinit.sas);
%loadPackage(BasePlus)
Title "Newly created macros";
proc sql;
create table After_processing as
select memname, objname, objtype
from dictionary.catalogs
where
objtype = "MACRO"
and libname = "WORK"
;
select * from After_processing
except
select * from Before_processing
;
quit;
Title;
Bart
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.