Previous Page | Next Page

Macro Functions for NLS

%KSUBSTR and %QKSUBSTR Macro Functions



Produce a substring of a character string.
Category: DBCS
Type: NLS macro function

Syntax
Details
Examples
Example 1: Limiting a Fileref to Eight Characters
Example 2: Storing a Long Macro Variable Value in Segments
Example 3: Comparing the Actions of %KSUBSTR and %QKSUBSTR

Syntax

%KSUBSTR (argument, position<, length>)
%QKSUBSTR (argument, position<, length>)

argument

is a character string or a text expression. If argument might contain a special character or mnemonic operator, listed here, use %QKSUBSTR.

position

is an integer or an expression (text, logical, or arithmetic) that yields an integer that specifies the position of the first character in the substring. If position is greater than the number of characters in the string, %KSUBSTR and %QKSUBSTR issue a warning message and return a null value.

length

is an optional integer or an expression (text, logical, or arithmetic) that yields an integer that specifies the number of characters in the substring. If length is greater than the number of characters following position in argument, %KSUBSTR and %QKSUBSTR issue a warning message and return a substring containing the characters from position to the end of the string. By default, %KSUBSTR and %QKSUBSTR produce a string containing the characters from position to the end of the character string.


Details

The %KSUBSTR and %QKSUBSTR functions produce a substring of argument, which begins at position and continues for the number of characters in length.

%KSUBSTR does not mask special characters or mnemonic operators in its result. %QKSUBSTR masks the following special characters and mnemonic operators:

& % ' " ( ) + - * / < > = ¬ ^ ~ ; , #  blank
AND OR NOT EQ NE LE LT GE GT IN


Examples


Example 1: Limiting a Fileref to Eight Characters

The macro MAKEFREF uses %KSUBSTR 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 %klength(&fileref) gt 8 %then
       %let fileref = %ksubstr(&fileref,1,8);
   filename &fileref "&file";
%mend makefref;

%makefref(humanresource,/dept/humanresource/report96)

SAS reads 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(%klength(&&msg&i)<40);
             %let msg&i=%qksubstr(&msg,&start,40);
             %put Message &i is: &&msg&i;
             %let i=%eval(&i+1);
             %let start=%eval(&start+40);
             %let msg&i=%qksubstr(&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 the Actions of %KSUBSTR and %QKSUBSTR

%KSUBSTR produces a resolved result because it does not mask special characters and mnemonic operators in the C language before processing it:

%let a=one;
%let b=two;
%let c=%nrstr(&a &b);

%put C: &c;
%put With KSUBSTR: %ksubstr(&c,1,2);
%put With QKSUBSTR: %qKsubstr(&c,1,2);

When these statements execute, these lines are written to the SAS log:

C: &a &b
With KSUBSTR: one
With QKSUBSTR: &a

Previous Page | Next Page | Top of Page