COMPARE Function

Returns the position of the leftmost character by which two strings differ, or returns 0 if there is no difference.

Category: Character
Restriction: I18N Level 0 functions are designed for use with Single Byte Character Sets (SBCS) only.
Tip: DBCS equivalent function is KCOMPARE in SAS National Language Support (NLS): Reference Guide. See also DBCS Compatibility .

Syntax

Required Arguments

string–1

specifies a character constant, variable, or expression.

string–2

specifies a character constant, variable, or expression.

Optional Argument

modifier

specifies a character string that can modify the action of the COMPARE function. You can use one or more of the following characters as a valid modifier:

i or I ignores the case in string–1 and string–2.
l or L removes leading blanks in string–1 and string–2 before comparing the values.
n or N removes quotation marks from any argument that is a name literal and ignores the case of string–1 and string–2. A name literal is a name token that is expressed as a string within quotation marks, followed by the uppercase or lowercase letter n. Name literals enable you to use special characters (including blanks) that are not otherwise allowed in SAS data set or variable names. For COMPARE to recognize a string as a name literal, the first character must be a quotation mark.
: (colon) truncates the longer of string–1 or string–2 to the length of the shorter string, or to one, whichever is greater. If you do not specify this modifier, the shorter string is padded with blanks to the same length as the longer string.
Tip
COMPARE ignores blanks that are used as modifiers.

Details

The Basics

The order in which the modifiers appear in the COMPARE function is relevant.
  • “LN” first removes leading blanks from each string, and then removes quotation marks from name literals.
  • “NL” first removes quotation marks from name literals, and then removes leading blanks from each string.
In the COMPARE function, if string–1 and string–2 do not differ, COMPARE returns a value of zero. If the arguments differ, then the following apply:
  • The sign of the result is negative if string–1 precedes string–2 in a sort sequence, and positive if string–1 follows string–2 in a sort sequence.
  • The magnitude of the result is equal to the position of the leftmost character at which the strings differ.

DBCS Compatibility

The DBCS equivalent function is KCOMPARE, which is documented in SAS National Language Support (NLS): Reference Guide. There are minor differences between the COMPARE and KCOMPARE functions. While both functions accept varying numbers of arguments, usage of the third argument is not compatible. The following example shows the differences in the syntax:
COMPARE(string-1, string-2 <, modifiers> )
KCOMPARE(string-1 <, position <, count> > , string-2)

Examples

Example 1: Understanding the Order of Comparisons When Comparing Two Strings

The following example compares two strings by using the COMPARE function.
data test;
   infile datalines missover;
   input string1 $char8. string2 $char8. modifiers $char8.;
   result=compare(string1, string2, modifiers);
   datalines;
1234567812345678
123     abc
abc     abx
xyz     abcdef
aBc     abc
aBc     AbC     i
   abc  abc
   abc  abc     l
 abc       abx
 abc       abx  l 
ABC     'abc'n
ABC     'abc'n  n 
 '$12'n $12     n
 '$12'n $12     nl
 '$12'n $12     ln
;

proc print data=test;
run;
Results of Comparing Two Strings by Using the COMPARE Function
Results of Comparing Two Strings by Using the COMPARE Function

Example 2: Truncating Strings Using the COMPARE Function

The following example uses the : (colon) modifier to truncate strings.
data test2;
   pad1=compare('abc','abc            ');   
   pad2=compare('abc','abcdef         ');   
   truncate1=compare('abc','abcdef',':');  
   truncate2=compare('abcdef','abc',':');  
   blank=compare('','abc',          ':');   
run;

proc print data=test2 noobs;
run;
Results of Using the Truncation Modifier
Results of Using the Truncation Modifier

See Also

CALL Routines: