BookmarkSubscribeRSS Feed
SasStatistics
Pyrite | Level 9

So far I have used %include statements to include a file containing several macros (around 100). By doing this, I can use the macros in other programs. But, I think that the standard (not sure if it is best practice?) of including macros is to use auto call libraries. In auto call libraries, every macro should be saved in a seperate file named as the macro. 

The file I included is schematically of the form: 

%macro MyMacro1; 
 /* some code */ 
%mend; 

%macro Macro2; 
 /* some code */ 
%mend; 

... 
...
...

%macro AnotherMacro; 
 /* some code */ 
%mend; 

Is there any way of splitting the schematic program above into seperate files containing the specific macro and where the file is named as the macro (this would make the switch to auto call easy). 

So, the first file would be called MyMacro1, the second Macro2 and so on. 

Thanks. 

 

6 REPLIES 6
PaigeMiller
Diamond | Level 26

@SasStatistics wrote:

Is there any way of splitting the schematic program above into seperate files containing the specific macro and where the file is named as the macro (this would make the switch to auto call easy). 

 


I can think of two ways

  1. manually
  2. write a program to parse the text
--
Paige Miller
SasStatistics
Pyrite | Level 9
2 would be very nice, do you have any advice on how to do this?
PaigeMiller
Diamond | Level 26

@SasStatistics wrote:
2 would be very nice, do you have any advice on how to do this?

No, I do not personally have advice on this. Perhaps some other member of the SAS Communities has done something like this.

--
Paige Miller
Tom
Super User Tom
Super User

Really no different than any program that need to read in data.

%let path=/where/I/want/to/write/files ;
%let infile=original.sas;

data _null_;
  infile "&infile" dlm=' (;/'  truncover ;
  length word1 macro $32 filename $50 fullname $256 ;
  retain filename 'header.txt';
  input word1 macro ;
  if upcase(word1)=:'%MACRO' then do;
     filename = cats(lowcase(macro),'.sas');
  end;
  fullname = catx('/',"&path",filename);
  file out filevar=fullname ;
  put _infile_;
  if upcase(word1)='%MEND' then filename='otherlines.sas';
run;  

So for your example you get this:

====================
anothermacro.sas
====================
%macro AnotherMacro;
 /* some code */
%mend;

====================
macro2.sas
====================
%macro Macro2;
 /* some code */
%mend;

====================
mymacro1.sas
====================
%macro MyMacro1;
 /* some code */
%mend;

====================
otherlines.sas
====================

...
...
...
ballardw
Super User

Some questions before spending any time on parsing such a file.

 

Are ALL comments /instructions related to use of specific macros included between the %macro/ %mend? If not any parsing program to create the files would likely lose such information and you may be better off doing the separation by hand (and place the comments /instructions inside the macro code block).

 

Are there any other program statements outside of the macro definitions? If so, what should happen to those?

 

Are there any places that an entire macro may be commented out such as

/*%macro dummy(someparm);*/
/*<programming statements>*/
/*%mend;*/

Or is there anyplace that %macro or %mend might be in a commented out statement such as:

%macro dummy (parm1, parm2);
/*%macro dummy(someparm);*/
<programming statements>
%mend;

The above may happen when a version has been modified and a the old macro statement left in for documentation reasons, still in process coding or lazy programmer not removing when completed.

 

The questions about the commented bits relate to possible "duplicate" macros found in the source file, the first one bit, or matching the correct start/end statements of a specific macro when parsing the text.

 

 

Tom
Super User Tom
Super User

Good points.  What I tell users to do when creating autocall macro source files is to have the %MACRO statement on the first line and the %MEND statement on the last line.  If they want have a standard program header then it should be after the %MACRO statement.

%macro xxx
/*----------------------------------------------------------------------
Short description
----------------------------------------------------------------------*/
(p1=P1     /* Parameter one description */
,p1=P1     /* Parameter two description */
);

/*----------------------------------------------------------------------
Usage:

one or more usage examples
------------------------------------------------------------------------
Notes:

additional information
-----------------------------------------------------------------------
History:

yyyy-mm-dd username comment
----------------------------------------------------------------------*/
%local macro parmerr ... other local macro variables;
%let macro=xxx;

%*----------------------------------------------------------------------
Validate macro parameters
-----------------------------------------------------------------------;
%parmv(P1,_req=1,_val=A B C)
%parmv(P2,_req=1)
%if (&parmerr) %then %goto quit;

%*----------------------------------------------------------------------
Use block style comment blocks.
To facilitate on-line browsing and printing, use only 72 columns.
-----------------------------------------------------------------------;
%quit:

%*----------------------------------------------------------------------
Abort the SAS job when errors in parameter specifications and option
ERRORABEND is turned on.
-----------------------------------------------------------------------;
%bailout(&parmerr)

%mend xxx;

If the large file has a consistent header block before the %MACRO statement it might be possible to detect and output those, but that would be more complicated.  

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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