From PhUSE Wiki
Jump to: navigation, search

Built-in macro-functions

Macro functions such as %INDEX, %SUBSTR(),or %LOWCASE() compute a result based on specified or default arguments, and return (or resolve to) a value, which is then used inside other user-written macro definitions or in open code such as data step, global statements and procedure calls. Nesting macro functions is also possible, i.e. the result of an inner macro function becomes the argument of an outer macro function. For example the following nested macro functions can be used to retrieve the 3 characters code of the month in lowercase from a DATE9. Formatted date:

  %put %LOWCASE(%SUBSTR(12OCT2015, 3, 3));

Which writes to the log:


Moreover, SAS macro language allows users to create new macro functions and extend the available functionality.

User-written macro-functions

For example:

%macro nicedate(date);
   %local datenum;
   %let datenum=%sysfunc(inputn(&date, ANYDTDTE.));
   %sysfunc(putn(&datenum, WEEKDATX.))
%let datex=%nicedate(&sysdate9); 
%put I ran this example on &datex [sysdate9=&sysdate9];

The log shows:

I ran this example on Wednesday, 2 September 2015 [sysdate9=02SEP2015]

In the example call, the current date from &sysdate9 is passed to macro %nicedate as macro-parameter &date, which is read by the nested function call %sysfunc(inputn()) into macro-variable &datenum as a numerical value using informat ANYDTDTE. Then &datenum value is formatted back to a character date with format WEEKDATX., by the nested function call %sysfunc(putn()). Note the returned value from this function call is not assigned to a macro variable within macro %nicedate, nor interpreted as a macro statement, and is also not followed by a semicolon; that value is then left unchanged by the macro language processor, and becomes itself the return value of the %nicedate() macro-function. The result of the macro function %nicedate is assigned to macro-variable &datex, which is printed to the log using the %put macro statement.