Chapter Contents

Previous

Next
%SYSFUNC and %QSYSFUNC

%SYSFUNC and %QSYSFUNC



Execute SAS functions or user-written functions

Type: Macro functions


Syntax
Details
Comparisons
Examples
Example 1: Formatting the Current Date in a TITLE Statement
Example 2: Formatting a Value Produced by %SYSFUNC
Example 3: Translating Characters
Example 4: Confirming the Existence of a SAS Data Set
Example 5: Determining the Number of Variables and Observations in a Data Set

Syntax

%SYSFUNC (function(argument(s))<,format>)
%QSYSFUNC (function(argument(s))<,format>)

function
is the name of the function to execute. This function can be a SAS function or a function written with SAS/TOOLKIT software. The function cannot be a macro function.

All SAS functions, except those listed in the table 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, for example,

%let x=%sysfunc(trim(%sysfunc(left(&num))));

Appendix 3, "Syntax for the Selected SAS Functions Used with %SYSFUNC," in SAS Macro Language: Reference, shows the syntax of SAS functions introduced with Release 6.12 used with %SYSFUNC.

argument(s)
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 the SAS System, generated by PROC FORMAT, or created with SAS/TOOLKIT. 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.


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:

All arguments in DATA step functions within %SYSFUNC must be separated by commas. You cannot use argument lists preceded by the word OF.

%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

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 they execute supports floating point numbers.

SAS Functions Not Available with %SYSFUNC and %QSYSFUNC
DIF DIM HBOUND
IORCMSG INPUT LAG
LBOUND MISSING PUT
RESOLVE SYMGET All Variable Information Functions

Note:   Intead of INPUT and PUT, which are not available with %SYSFUNC and %QSYSFUNC, use INPUTN, INPUTC, PUTN, and PUTC in Screen Control Language.  [cautionend]

Note:   The Variable Information functions include functions such as VNAME and VLABEL. For a complete list, see "Functions and CALL Routines" in SAS Language Reference: Dictionary.  [cautionend]

CAUTION:
Values returned by SAS functions may 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.  [cautionend]


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";

Executing this statement on July 18, 2000, produces this TITLE statement:

title "July 18, 2000 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)

Executing this program writes these lines 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;

Executing these statements writes these lines 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)

Executing this program produces the statements:

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 utilized 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,nvarsp,nobsp);
   %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));
      %end;
   %else
      %put Open for data set &dset failed - %sysfunc(sysmsg());
%mend obsnvars;

%obsnvars(sasuser.houses,nvars,nobs)

%put &dset has &nvars variable(s) and &nobs observation(s).;

Executing this program writes these lines to the SAS log:

sasuser.houses has 6 variable(s) and 15 observation(s).


Chapter Contents

Previous

Next

Top of Page

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