STRCMP: Comparing Character Strings

How to:

The STRCMP function compares two character strings using the EBCDIC or ASCII collating sequence.

Syntax: How to Compare Character Strings

STRCMP(string1, string2)

where:

string1, string2

Alphanumeric

Are the strings to compare, or temporary fields that contain the strings.

Example: Comparing Character Strings

STRCMP compares the length of two fields:

MAINTAIN
COMPUTE STR1/A20 = 'STRING IS LONG';
        STR2/A20 = 'STRING IS LONGER';
COMPUTE DIF/I3= STRCMP(STR1, STR2);
TYPE "STR1 = <<STR1"
TYPE "STR2 = <<STR2"
IF DIF LT 0 THEN TYPE "STR2 IS GREATER THAN STR1"
ELSE IF DIF GT 0 THEN TYPE "STR2 IS LESS THAN STR1"
ELSE IF DIF EQ 0 THEN TYPE "STR2 EQUALS STR1"
TYPE " "
COMPUTE STR3/A20 = 'STRING IS LONGEST';
        STR4/A20 = 'STRING IS LONG';
TYPE "STR3 = <<STR3"
TYPE "STR4 = <<STR4"
COMPUTE DIF= STRCMP(STR3, STR4);
IF DIF LT 0 THEN TYPE "STR4 IS GREATER THAN STR3"
ELSE IF DIF GT 0 THEN TYPE "STR4 IS LESS THAN STR3"
ELSE IF DIF EQ 0 THEN TYPE "STR4 EQUALS STR3"
TYPE " "
COMPUTE DIF= STRCMP(STR1, STR4);
IF DIF LT 0 THEN TYPE "STR1 IS GREATER THAN STR4"
ELSE IF DIF GT 0 THEN TYPE "STR1 IS LESS THAN STR4"
ELSE IF DIF EQ 0 THEN TYPE "STR1 EQUALS STR4"
END

The result is:

STR1 = STRING IS LONG
STR2 = STRING IS LONGER
STR2 IS GREATER THAN STR1
STR3 = STRING IS LONGEST
STR4 = STRING IS LONG
STR4 IS LESS THAN STR3
STR1 EQUALS STR4