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:
%SYSFUNC and %QSYSFUNC Functions 285
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;
286 Chapter 17 Macro Functions
%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).
%SYSGET Function
Returns the value of the specified operating environment variable.
Type: Macro function
See: “Macro Functions in UNIX Environments” in SAS Companion for UNIX Environments
“Macro Functions” in SAS Companion for Windows
“Macro Functions” in SAS Companion for z/OS
Syntax
%SYSGET(environment-variable)
Required Argument
environment-variable
is the name of an environment variable. The case of environment-variable must agree
with the case that is stored on the operating environment.
Details
The %SYSGET function returns the value as a character string. If the value is truncated
or the variable is not defined on the operating environment, %SYSGET displays a
warning message in the SAS log.
You can use the value returned by %SYSGET as a condition for determining further
action to take or parts of a SAS program to execute. For example, your program can
restrict certain processing or issue commands that are specific to a user.
For more information, see the SAS documentation for your operating environment.
Example: Using SYSGET in a UNIX Operating
Environment
This example returns the ID of a user on a UNIX operating environment:
%let person=%sysget(USER);
%put User is &person;
When these statements execute for user ABCDEF, the following is written to the SAS
log:
User is abcdef
%SYSGET Function 287
%SYSMACEXEC Function
Returns an indication of the execution status of a macro.
Type: Macro function
Syntax
%SYSMACEXEC(macro_name)
Required Argument
macro_name
the name of a macro or a text expression that yields the name of the macro.
Details
The %SYSMACEXEC function returns the number 1 if the macro is currently
executing. Otherwise, if the macro is not executing, the number 0 is returned.
%SYSMACEXIST Function
Returns an indication of the existence of a macro definition in the Work.SASMacr catalog. Otherwise, the
returned value is 0.
Type: Macro function
Syntax
%SYSMACEXIST(macro-name)
Required Argument
macro-name
the name of a macro or a text expression that yields the name of a macro.
Details
The %SYSMACEXIST function returns the number 1 if a definition for the macro exists
in the Work.SASMacr catalog. If there is not a macro definition, the returned value is 0.
%SYSMEXECDEPTH Function
Returns the nesting depth of macro execution from the point of the call to %SYSMEXECDEPTH.
Type: Macro Function
Tip: %SYSMEXECDEPTH and %SYSMEXECNAME were implemented to be used
together, but it is not required.
See: %SYSMEXECNAME Function
288 Chapter 17 Macro Functions
Syntax
%SYSMEXECDEPTH
Details
To retrieve the nesting level of the currently executing macro, use the
%SYSMEXECDEPTH. This function returns a number indicating the depth of the
macro in nested macro calls. The following are the %SYSMEXECDEPTH return value
descriptions:
0 open code
>0 nesting level
See the following example and explanations that follow it.
8 %macro A;
9 %put %sysmexecdepth;
10 %mend A; /* The macro execution depth
of a macro called from open code */
11 %A; /* is one */
1
12
13 %macro B;
14 %put %nrstr(%%)sysmexecdepth=%sysmexecdepth;
15 %put %nrstr(%%)sysmexecname(1)=%sysmexecname(1);
16 %put %nrstr(%%)sysmexecname(2)=%sysmexecname(2);
17 %put %nrstr(%%)sysmexecname(0)=%sysmexecname(0);
18 %put %nrstr(%%)sysmexecname(%nrstr(%%)sysmexecdepth-1)=
%sysmexecname(%sysmexecdepth-1);
19 %mend B;
20
21 %macro C;
22 %B;
23 %mend;
24 %C;
%sysmexecdepth=2
%sysmexecname(1)=C
%sysmexecname(2)=B
%sysmexecname(0)=OPEN CODE
%sysmexecname(%sysmexecdepth-1)=C
25
26 %macro level1;
27 %level2;
28 %mend;
29 %macro level2;
30 %level3;
31 %mend;
32 %macro level3;
33 %level4;
34 %mend;
35 %macro level4;
36 %do i = %sysmexecdepth+1 %to -1 %by -1;
37 %put %nrstr(%%)sysmexecname(&i)=%sysmexecname(&i);
38 %end;
39 %mend;
%SYSMEXECDEPTH Function 289
..................Content has been hidden....................

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