%SYSFUNC and %QSYSFUNC Functions

Execute SAS functions or user-written functions.
Type: Macro function
Tip: %SYSFUNC and %QSYSFUNC support SAS function names up to 32 characters.

Syntax

%SYSFUNC (function(argument-1 <...argument-n>)<, format> )
%QSYSFUNC (function(argument-1 <...argument-n>)<, format> )

Required Arguments

function
is the name of the function to execute. This function can be a SAS function, a function written with SAS/TOOLKIT software, or a function created using the FCMP Procedure in Base SAS Procedures Guide. The function cannot be a macro function.
All SAS functions, except those listed in SAS Functions Not Available with %SYSFUNC and %QSYSFUNC, can be used with %SYSFUNC and %QSYSFUNC.
You cannot nest functions to be used with a single %SYSFUNC. However, you can nest %SYSFUNC calls:
%let x=%sysfunc(trim(%sysfunc(left(&num))));
Syntax for Selected Functions Used with the %SYSFUNC Function shows the syntax of SAS functions used with %SYSFUNC that were introduced with SAS 6.12.
argument-1 <...argument-n>
is one or more arguments used by function. An argument can be a macro variable reference or a text expression that produces arguments for a function. If argument might contain a special character or mnemonic operator, listed below, use %QSYSFUNC.
format
is an optional format to apply to the result of function. This format can be provided by SAS, generated by PROC FORMAT, or created with SAS/TOOLKIT. There is no default value for format. If you do not specify a format, the SAS macro facility does not perform a format operation on the result and uses the default of the function.

Details

Because %SYSFUNC is a macro function, you do not need to enclose character values in quotation marks as you do in DATA step functions. For example, the arguments to the OPEN function are enclosed in quotation marks when the function is used alone, but do not require quotation marks when used within %SYSFUNC. These statements show the difference:
  •  dsid=open("sasuser.houses","i");
  •  dsid=open("&mydata","&mode");
  •  %let dsid = %sysfunc(open(sasuser.houses,i));
  •  %let dsid=%sysfunc(open(&mydata,&mode));
All arguments in DATA step functions within %SYSFUNC must be separated by commas. You cannot use argument lists preceded by the word OF.
Note: The arguments to %SYSFUNC are evaluated according to the rules of the SAS macro language. This includes both the function name and the argument list to the function. In particular, an empty argument position will not generate a NULL argument, but a zero length argument.
%SYSFUNC does not mask special characters or mnemonic operators in its result. %QSYSFUNC masks the following special characters and mnemonic operators in its result:
& % ' " ( ) + − * / < > = ¬ ^ ~ ; , #  blank
AND OR NOT EQ NE LE LT GE GT IN
When a function called by %SYSFUNC or %QSYSFUNC requires a numeric argument, the macro facility converts the argument to a numeric value. %SYSFUNC and %QSYSFUNC can return a floating point number when the function that they execute supports floating point numbers.
SAS Functions Not Available with %SYSFUNC and %QSYSFUNC
All Variable Information Functions
ALLCOMB
ALLPERM
DIF
DIM
HBOUND
IORCMSG
INPUT
LAG
LBOUND
LEXCOMB
LEXCOMBI
LEXPERK
LEXPERM
MISSING
PUT
RESOLVE
SYMGET
Note: Instead of INPUT and PUT, which are not available with %SYSFUNC and %QSYSFUNC, use INPUTN, INPUTC, PUTN, and PUTC.
Note: The Variable Information functions include functions such as VNAME and VLABEL. For a complete list, see “Definitions of Functions and CALL Routines” in SAS Functions and CALL Routines: Reference.
CAUTION:
Values returned by SAS functions might be truncated.
Although values returned by macro functions are not limited to the length imposed by the DATA step, values returned by SAS functions do have that limitation.

Comparisons

%QSYSFUNC masks the same characters as the %NRBQUOTE function.

Examples

Example 1: Formatting the Current Date in a TITLE Statement

This example formats a TITLE statement containing the current date using the DATE function and the WORDDATE. format:
title "%sysfunc(date(),worddate.) Absence Report";
When the program is executed on July 18, 2008, the statement produces the following TITLE statement:
title "July 18, 2008 Absence Report"

Example 2: Formatting a Value Produced by %SYSFUNC

In this example, the TRY macro transforms the value of PARM using the PUTN function and the CATEGORY. format.
proc format;
  value category
  Low-<0  = 'Less Than Zero'
  0       = 'Equal To Zero'
  0<-high = 'Greater Than Zero'
  other   = 'Missing';
run;
%macro try(parm);
  %put &parm is %sysfunc(putn(&parm,category.));
%mend;
%try(1.02)
%try(.)
%try(-.38)
When these statements are executed, these lines are written to the SAS log:
1.02 is Greater Than Zero
. is Missing
-.38 is Less Than Zero

Example 3: Translating Characters

%SYSFUNC executes the TRANSLATE function to translate the Ns in a string to Ps.
%let string1 = V01N01-V01N10;
%let string1 = %sysfunc(translate(&string1,P, N));
%put With N translated to P, V01N01-V01N10 is &string1;
When these statements are executed, these lines are written to the SAS log:
With N translated to P, V01N01-V01N10 is V01P01-V01P10

Example 4: Confirming the Existence of a SAS Data Set

The macro CHECKDS uses %SYSFUNC to execute the EXIST function, which checks the existence of a data set:
%macro checkds(dsn);
   %if %sysfunc(exist(&dsn)) %then
      %do;
         proc print data=&dsn;
         run;
      %end;
      %else
         %put The data set &dsn does not exist.;
%mend checkds;
%checkds(sasuser.houses)
When the program is executed, the following statements will be produced:
PROC PRINT DATA=SASUSER.HOUSES;
RUN;

Example 5: Determining the Number of Variables and Observations in a Data Set

Many solutions have been generated in the past to obtain the number of variables and observations present in a SAS data set. Most past solutions have used a combination of _NULL_ DATA steps, SET statement with NOBS=, and arrays to obtain this information. Now, you can use the OPEN and ATTRN functions to obtain this information quickly and without interfering with step boundary conditions.
%macro obsnvars(ds);
   %global dset nvars nobs;
   %let dset=&ds;
   %let dsid = %sysfunc(open(&dset));
   %if &dsid %then
      %do;
         %let nobs =%sysfunc(attrn(&dsid,NOBS));
         %let nvars=%sysfunc(attrn(&dsid,NVARS));
         %let rc = %sysfunc(close(&dsid));
         %put &dset has &nvars  variable(s) and &nobs observation(s).;
      %end;
   %else
      %put Open for data set &dset failed - %sysfunc(sysmsg());
%mend obsnvars;
%obsnvars(sasuser.houses)
When the program is executed, the following message will appear in the SAS log:
sasuser.houses has 6 variable(s) and 15 observation(s).