Using SAS Functions with Macro Variables

The %SYSFUNC Function

You have learned that by using the automatic macro variables SYSDATE9 and SYSTIME you can include the date and time in a title:
title1 "Report Produced on &sysdate9";
title2 "at &systime";
Report Produced on &sysdate9
SYSDATE9 represents the date on which the SAS session started, and SYSTIME represents the time at which the SAS session started. Suppose you would rather see the date in some other format, or suppose you would rather see the current date or time. You can use the %SYSFUNC function to execute other SAS functions as part of the macro facility.
General form, %SYSFUNC function:
%SYSFUNC (function (argument(s)) <,format>)
Here is an explanation of the syntax:
function
is the name of the SAS function to execute.
argument(s)
is one or more arguments that are used by function. Use commas to separate all arguments. An argument can be a macro variable reference or a text expression that produces arguments for a function.
format
is an optional format to apply to the result of function. By default, numeric results are converted to a character string using the BEST12. format, and character results are used as they are, without formatting or translation.
 All SAS functions can be used with %SYSFUNC except
ALLCOMB LEXCOMB
ALLPERM LEXCOMBI
DIF LEXPERK
DIM LEXPERM
HBOUND MISSING
INPUT PUT
IORCMSG RESOLVE
LAG SYMGET
LBOUND Variable information functions
Note: Variable information functions include functions such as VNAME and VLABEL. For a complete list of variable information functions, see "Functions and CALL Routines by Category" in SAS Functions and CALL Routines: Reference.
Note: You can use the INPUTC or INPUTN function in place of the INPUT function. Similarly, you can use the PUTC or PUTN function in place of the PUT function with %SYSFUNC.

Example

Suppose the following code was submitted on Friday, November 4, 2011:
    title "%sysfunc(today(),weekdate.) - SALES REPORT";
The title on the next report would be Friday, November 4, 2011 - SALES REPORT.

Quoting with %QSYSFUNC

As with macro character functions, if the argument for a %SYSFUNC function contains special characters or mnemonic operators, you must use the quoting version of the function. The %QSYSFUNC function has the same syntax as the %SYSFUNC function. %QSYSFUNC works the same as %SYSFUNC except that it also masks mnemonic operators and special characters in the function result.

Example

Suppose you want to create a report title that includes the current date in WORDDATE. format. You could use this statement:
 title "Report Produced on %sysfunc(today(),worddate.)";
However, that would result in the following title:
Report Produced on   November 4, 2011
Note: The extra blanks are displayed in the listing output. The blanks are not displayed in an HTML output.
The extra blanks are from the default length of the WORDDATE. format. You need to left-justify the resulting formatted date. You cannot nest functions within %SYSFUNC, but you can use a %SYSFUNC for each function that you need, as shown in this example:
title "Report Produced on 
              %sysfunc(left(%sysfunc(today(),worddate.)))";
 
However, this statement results in the following error message.
Table 9.13 SAS Log
ERROR: The function LEFT referenced by the %SYSFUNC or
       %QSYSFUNC macro function has too many arguments.
The LEFT function expects only one argument, but you are passing “November 4, 2011” to it. It interprets the comma as the delimiter between two arguments.
You can mask the comma by using the %QSYSFUNC function instead, as follows:
 title "Report Produced on 
               %sysfunc(left(%qsysfunc(today(),worddate.)))";
The modified statement generates the following title:
Report Produced on November 4, 2011
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.28.108