Examples
Example 1: Limiting a Fileref to Eight Characters
The macro MAKEFREF uses %SUBSTR to assign the first eight characters of a
parameter as a fileref, in case a user assigns one that is longer.
%macro makefref(fileref,file);
%if %length(&fileref) gt 8 %then
%let fileref = %substr(&fileref,1,8);
filename &fileref "&file";
%mend makefref;
%makefref(humanresource,/dept/humanresource/report96)
SAS sees the following statement:
FILENAME HUMANRES "/dept/humanresource/report96";
Example 2: Storing a Long Macro Variable Value in Segments
The macro SEPMSG separates the value of the macro variable MSG into 40-character
units and stores each unit in a separate variable.
%macro sepmsg(msg);
%let i=1;
%let start=1;
%if %length(&msg)>40 %then
%do;
%do %until(%length(&&msg&i)<40);
%let msg&i=%qsubstr(&msg,&start,40);
%put Message &i is: &&msg&i;
%let i=%eval(&i+1);
%let start=%eval(&start+40);
%let msg&i=%qsubstr(&msg,&start);
%end;
%put Message &i is: &&msg&i;
%end;
%else %put No subdivision was needed.;
%mend sepmsg;
%sepmsg(%nrstr(A character operand was found in the %EVAL function
or %IF condition where a numeric operand is required. A character
operand was found in the %EVAL function or %IF condition where a
numeric operand is required.));
When this program executes, these lines are written to the SAS log:
Message 1 is: A character operand was found in the %EV
Message 2 is: AL function or %IF condition where a nu
Message 3 is: meric operand is required. A character
Message 4 is: operand was found in the %EVAL function
Message 5 is: or %IF condition where a numeric operan
Message 6 is: d is required.
Example 3: Comparing Actions of %SUBSTR and %QSUBSTR
Because the value of C is masked by %NRSTR, the value is not resolved at compilation.
%SUBSTR produces a resolved result because it does not mask special characters and
mnemonic operators in C before processing it, even though the value of C had
previously been masked with the %NRSTR function.
%let a=one;
276 Chapter 17 • Macro Functions