1(USEREXIT) 0USEREXIT: CODING SAS USER EXIT MACROS ------------------------------------- The term "user exit macro" is our own invention. The entity which it describes, however, is widely used as a tool to provide greater flexibility and user control over applications written in the SAS preprocessor macro language. As its name implies, a user exit macro is a SAS preprocessor macro which is written by a user who wants to provide some customized processing to take place during the execution of a larger utility macro. User exit macros are almost always optional: they rarely are required as a necessary part of the invoking macro's logic. The name of the exit macro is usually specified via a parameter defined within the invoking macro. The user must first define his exit macro, and then must tell the invoking macro that he wants it executed by supplying the name of his exit macro as the value of a parameter. 0 For example, suppose we have the following very simply "utility" macro called PRINT: %MACRO PRINT(DSIN=_LAST_,OBS=99999,USERMAC=); DATA; SET &DSIN (OBS=&OBS); %IF &USERMAC NE %STR( ) %THEN %&USERMAC; PROC PRINT; %MEND PRINT; The line of code after the DATA statement will be processed by the macro preprocessor as follows: the value of the parameter USERMAC will be checked to see if it is null (this is the default value that will apply if the user says nothing about USERMAC when invoking PRINT.) If no value is specified then no SAS code gets generated by the %IF statement. However, if the user specifies a value for the USERMAC parm when he invokes PRINT it results in the following SAS source code being generated: %; where is the value specified for the USERMAC parm. Inother- words the value of the USERMAC parm is the name of a preprocessor macro that gets invoked. The user is totally responsible for defining this macro, of course. The user exit macro is simply a "hook" that allows the user to insert some custom code in the middle of an other- wise standard processing module. As an example of how to code a user exit macro lets suppose we want to use the PRINT macro as defined above on a dataset named TRACTS, but that we only want to print observations in which the value of the variable TOTPOP is at least 5000. We could accomplish this using the following code: %MACRO SELECT; IF TOTPOP GE 5000; %MEND SELECT; *--THE USER EXIT MACRO IS NOW DEFINED--*; %PRINT(DSIN=TRACTS,USERMAC=SELECT); *--INVOKES THE UTILITY MACRO WHICH WILL IN TURN INVOKE THE %SELECT MACRO IN ORDER TO FILTER THE DATA TO BE PRINTED.---*;