This is the third post in our SAS Macro Facility series and this post will make a summary of macro functions.

Other posts of SAS Macro Facility series

An overall summary of SAS Macro Facility – Part 1 (Macro variable)
An overall summary of SAS Macro Facility – Part 2 (Macro program)

Macro functions

Macro functions can extend the use of macro variables and macro programming and can be used in both open code and macro programs. The arguments of a macro function can be something such as strings, macro variables, macro functions and even macro program calls. As for macro function results, they can only be text. There are five categories of macro functions: macro character functions, macro evaluation functions, macro quoting functions, macro variable attribute functions and other macro functions.

Character functions, Evaluation functions and Macro variable attribute functions

Here are a list of syntax and corresponding explainations for several macro functions.

 

 

 

 

 

 

 

 

 

Character

%INDEX(source, string) Return the position of the first character of string in source.
%LENGTH(string|text expression) Return the length of string or the length of the results of the resolution of text expression.
%SCAN(argument, n <,delimiters>) Return the nth word in argument where the words in argument are separated by delimiters.
%QSCAN(argument, n <,delimiters>) Does the same thing as %SCAN with the addition of masking special characters and mnemonic operators
%SUBSTR(argument,position<,length>) Extract a substring of length characters from argument starting at position.
%QSUBSTR(argument,position<,length>) Does the same thing as %SUBSTR with the addition of masking special characters and mnemonic operators
%UPCASE(string|text expression) Converts character string or text expression to uppercase.
%QUPCASE(string|text expression) Does the same thing as %UPCASE with the addition of masking special characters and mnemonic operators
Evaluation %EVAL(arithmetic|logical expression) Evaluate expressions using integer arithmetic.
%SYSEVALF(arithmetic|logical expression <,conversion-type>) Evaluate expressions using floating point arithmetic.
Macro Variable Attribute %SYMEXIST(macro-variable-name) Return 1 if the named macro variable exists. Otherwise, return 0.
%SYMGLOBL(macro-variable-name) Return 1 if the named macro variable exists in global symbol table. Otherwise, return 0.
%SYMLOCAL(macro-variable-name) Return 1 if the named macro variable exists in a local symbol table. Otherwise, return 0.

Macro Quoting Functions

Macro quoting functions is very important if you want the macro processor to not interpret special characters such as percent sign (%) and ampersand sign (&).

Other Macro Functions

When you want to apply SAS language functions such as MEAN, PUT to macro variables or text, you can apply other macro functions like %SYSFUNC and %QSYSFUNC.

%SYSFUNC(function(argument<,format>)) Executes SAS language function or user-written function and returns the results to the macro facility
%QSYSFUNC(function(argument<,format>)) Executes SAS language function or user-written function and returns the results to the macro facility

Mask special characters and mnemonic operators in the result

%SYSGET Returns the value of host-environment-variables to macro facility

Autocall Macro Program that can be used like functions

There are some SAS supplied uncompiled macro code which can be used like functions.

%CMPRES(text|text expression) Remove leading and trailing blanks from the argument
%QCMPRES(text|text expression) Does the same thing as %CMPRES with the addition of masking special characters and mnemonic operators
%LEFT(text|text expression) Removing leading blanks from the argument
%QLEFT(text|text expression) Does the same thing as %LEFT with the addition of masking special characters and mnemonic operators
%LOWCASE(text|text expression) Change a value to lowercase.
%QLOWCASE(text|text expression) Does the same thing as %LOWCASE with the addition of masking special characters and mnemonic operators
%DATATYP(text|text expression) Returns the data type (CHAR or NUMERIC) of a value
%VERIFY(text1|text2) Returns the position of the first character unique to one of the text.