Functions and CALL Routines |
Category: | Character |
Restriction: | I18N Level 0 |
Tip: | DBCS equivalent function is KCOMPARE in SAS National Language Support (NLS): Reference Guide. See also DBCS Compatibility. |
Syntax |
COMPARE(string-1, string-2<,modifiers>) |
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:
Tip: | COMPARE ignores blanks that are used as modifiers. |
Details |
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.
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 |
The following example compares two strings by using the COMPARE function.
options pageno=1 nodate ls=80 ps=60; 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;
The following output shows the results.
Results of Comparing Two Strings by Using the COMPARE Function
The SAS System 1 Obs string1 string2 modifiers result 1 12345678 12345678 0 2 123 abc -1 3 abc abx -3 4 xyz abcdef 1 5 aBc abc -2 6 aBc AbC i 0 7 abc abc -1 8 abc abc l 0 9 abc abx 2 10 abc abx l -3 11 ABC 'abc'n 1 12 ABC 'abc'n n 0 13 '$12'n $12 n -1 14 '$12'n $12 nl 1 15 '$12'n $12 ln 0
The following example uses the : (colon) modifier to truncate strings.
options pageno=1 nodate ls=80 pagesize=60; 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;
The following output shows the results.
Results of Using the Truncation Modifier
The SAS System 1 pad1 pad2 truncate1 truncate2 blank 0 -4 0 0 -1
See Also |
|
Copyright © 2011 by SAS Institute Inc., Cary, NC, USA. All rights reserved.