You are on page 1of 6

Lesson 2: Using Macro Functions Summary Main Points

Overview of Macro Functions %function-name (argument-1<,argument-n>) Macro functions enable you to manipulate text strings that SAS inserts in your code. When you submit a program, SAS executes the macro functions before the program compiles. To use a macro function, specify the function name, which starts with a percent sign. Enclose the function arguments in parentheses, separated by commas. The arguments can include constant text, macro variable references, macro functions, and macro calls. When you use constant text, do not enclose the text in quotation marks. You can use all macro functions in both open code and macro definitions. Macro functions are categorized in four types: macro character functions, macro evaluation functions, macro quoting functions, and other macro functions.

Using Macro Character Functions %SUBSTR(argument, position <, n>) Macro character functions enable you to manipulate character strings or obtain information about them. %SUBSTR extracts a substring of characters from an argument consisting of a character string or text expression. Position specifies where the extraction should begin. n specifies the number of characters to extract. If you don't specify n, the remainder of the string is extracted.

%SCAN(argument, n <, delimiters>) The %SCAN function enables you to extract words from a macro variable or text expression. %SCAN returns the nth word in an argument, where the words in the argument are separated by delimiters. If n is greater than the number of words in the argument, the function returns a null string. Delimiters refers to the characters that separate words or text expressions. If you omit the optional delimiter information, %SCAN uses a default set of delimiters shown below.

SAS Macro Language 1: Essentials


Copyright 2010 SAS Institute Inc., Cary, NC, USA. All rights reserved.

Lesson 2: Using Macro Functions


Encoding Type ASCII EBCDIC Default Delimiters blank . < ( + & ! $ * ) ; ^ - / , % | blank . < ( + | & ! $ * ) ; - / , %

%UPCASE(argument) The %UPCASE function enables you to convert characters to uppercase before substituting that value in a SAS program.

%INDEX(source,string) The %INDEX function enables you to search for a substring within a string. %INDEX searches source for the first occurrence of string and returns the position of its first character. If an exact match of string is not found, the function returns 0.

Using Arithmetic and Logical Expressions %EVAL (arithmetic or logical expression) The %EVAL function evaluates arithmetic and logical expressions.
Arithmetic Expressions 1+2 4*3 4/2 00FFx - 003Ax Logical Expressions &DAY = FRIDAY A<a 1 < &INDEX &START NE &END

When %EVAL evaluates an arithmetic expression, it temporarily converts operands to numeric values and performs an integer arithmetic operation. If the result of the expression is noninteger, %EVAL truncates the value to an integer. The result is expressed as text. The %EVAL function generates an error message in the log when it encounters an expression that contains noninteger values. When %EVAL evaluates a logical expression, it returns a value of 0 to indicate that the expression is false, or a value of 1 to indicate that the expression is true.

SAS Macro Language 1: Essentials

Lesson 2: Using Macro Functions


%SYSEVALF (expression <, conversion-type>) The %SYSEVALF function evaluates arithmetic and logical expressions using floating-point arithmetic and returns a value that is formatted using the BEST32. format. The result of the evaluation is always text. You can use %SYSEVALF with an optional conversion type (BOOLEAN, CEIL, FLOOR, or INTEGER) that tailors the value returned by %SYSEVALF.

Using SAS Functions with Macro Variables %SYSFUNC(function(argument(s)) <, format>) You can use the %SYSFUNC macro function to execute SAS functions within the macro facility. Because %SYSFUNC is a macro function, you don't enclose character values in quotation marks, as you do in SAS functions. You can specify an optional format for the value returned by the function. If you do not specify a format, numeric results are converted to a character string using the BEST12. format. SAS returns character results as they are, without formatting or translation. You can use almost any SAS function with the %SYSFUNC macro function. The exceptions are shown in this table.
Function Type Array processing Variable information Macro interface Data conversion Other functions Function Name DIM, HBOUND, LBOUND VNAME, VLABEL, MISSING RESOLVE, SYMGET INPUT, PUT* ORCMSG, LAG, DIF

*Use INPUTC and INPUTN in place of INPUT, and PUTC and PUTN in place of PUT. Using Macro Functions to Mask Special Characters %STR(argument) Macro quoting functions enable you to clearly indicate to the macro processor how it is to interpret special characters and mnemonics.

SAS Macro Language 1: Essentials

Lesson 2: Using Macro Functions


The macro quoting function %STR masks (or quotes) special characters during compilation so that the macro processor does not interpret them as macro-level syntax. %STR can also be used to quote tokens that typically occur in pairs, such as the apostrophe, quotation marks, and open and closed parentheses. Here is a list of all of the special characters and mnemonics masked by %STR.
+ - * / < > = ^ ~ ; , # blank AND OR NOT EQ NE LE LT GE GT IN ' " ) (

Note that %STR does not mask the characters & or %.

%NRSTR(argument) %NRSTR masks the same characters as %STR and also masks the special characters & and %. Using %NRSTR instead of %STR prevents macro and macro variable resolution.

SAS Macro Language 1: Essentials

Lesson 2: Using Macro Functions

Sample Code
Manipulating Character Strings Using Macro Functions %let %let %let %let %let dsn=orion.orders; var=order_date; name=%scan(&dsn,2,.); startYear=2007; curr_year=%substr(&sysdate9,6);

data &name; set &dsn; where &startYear <= year(&var) <= &curr_year; run; title "Listing of %upcase(&name) Data for &startYear-&curr_year"; proc print data=&syslast; run; Using %INDEX to Search for a Substring %let sitelist=DALLAS SEATTLE BOSTON; %let value=%index(&sitelist,LA); Using %EVAL to Evaluate an Arithmetic Expression %let thisyr=%substr(&sysdate9,6); %let lastyr=%eval(&thisyr-1); proc means data=work.order_fact maxdec=2 min max mean; class order_type; var total_retail_price; where year(order_date) between &lastyr and &thisyr; title1 "Orders for &lastyr and &thisyr"; title2 "(as of &sysdate9)"; run;

SAS Macro Language 1: Essentials

Lesson 2: Using Macro Functions


Using %SYSEVALF to Evaluate an Arithmetic Expression Tthat Includes a Noni Integer %let a=100; %let b=1.59; %let y=%sysevalf(&a+&b); %put %put %put %put %put The result with SYSEVALF is: &y; BOOLEAN conversion: %sysevalf(&a +&b, boolean); CEIL conversion: %sysevalf(&a +&b, ceil); FLOOR conversion: %sysevalf(&a +&b, floor); INTEGER conversion: %sysevalf(&a +&b, integer);

Using %SYSFUNC to Execute SAS Functions title1 "Report Produced on %sysfunc(today(),weekdate.)"; title2 "at %sysfunc(time(),timeAMPM8.)"; Using %STR to Mask Special Characters %let prt=%str(proc print data=&syslast; run;); Using %NRSTR to Mask Macro Triggers %let statement=%nrstr(title "S&P 500";);

SAS Macro Language 1: Essentials

You might also like