Sunday, January 9, 2011

SAS macro string functions

SAS offers a rather short list of macro functions for string manipulation. Unlike regular SAS string functions (the ones used in a data set, without '%' in the front), these functions can be used directly in macro programming.
  • %INDEX(source, string): Returns the position of the first character of a string.
  • %SCAN(source, n, string, modifier): Search for the nth word in a string. Use modifier to add search options.
  • %QSCAN: same as %SCAN but does not resolve special characters such as % and &.
  • %SUBSTR(source, n, length): produce a substring of argument, beginning at position, for length number of characters.
  • %QSUBSTR: same as %SUBSTR but does not resolve special characters such as % and &.
  • %BQUOTE: Mark special characters ' " ( ) + - * / < > = ¬ ^ ~ ; , # blank AND OR NOT EQ NE LE LT GE GT IN
  • %NRBQUOTE: same as %BQUOTE but in addition mark: & %.
The complete list of SAS macro functions can be found at: http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a003167026.htm

With such short list of function, you may not always find the functions you want to use. What if you want to used a regular data step string function such as lowcase() or anydigit()? Well, you can always use %sysfunc() function to 'borrow' the data step functions into the macros.
The complete list of SAS string functions can be found at (PDF file):
http://support.sas.com/publishing/pubcat/chaps/59343.pdf

No comments:

Post a Comment